Python Programming With Function Def and Else/If Commands Watch

beanboy25
Badges: 2
Rep:
?
#1
Report Thread starter 1 year ago
#1
I have made a program to find your taxed amount of money, and then add a tip on top. I am using a def command to hold the main part of the program. (I.E. The part where it asks for your tip.) Just need somebody to help with else command problems. And some def commands problems. Thanks!
0
reply
beanboy25
Badges: 2
Rep:
?
#2
Report Thread starter 1 year ago
#2
(Original post by beanboy25)
I have made a program to find your taxed amount of money, and then add a tip on top. I am using a def command to hold the main part of the program. (I.E. The part where it asks for your tip.) Just need somebody to help with some problems. Thanks!
Name:  screenshot.png
Views: 23
Size:  142.8 KB


this is what happens when i run it
0
reply
winterscoming
Badges: 19
Rep:
?
#3
Report 1 year ago
#3
(Original post by beanboy25)
I have made a program to find your taxed amount of money, and then add a tip on top. I am using a def command to hold the main part of the program. (I.E. The part where it asks for your tip.) Just need somebody to help with some problems. Thanks!
Could you elaborate on the actual problem(s) you're having? Any error messages? Is the program running without errors but behaving differently to the way you expect?

Try creating an example which reproduces the problem using repl.it at https://repl.it/languages/python - include an explanation of what it is you're actually trying to achieve, also details such as what input(s) you're using and your expected-vs-actual output.
0
reply
beanboy25
Badges: 2
Rep:
?
#4
Report Thread starter 1 year ago
#4
code:

print "Welcome \nEnter The Amount Of Money For the Transaction" amount = float(raw_input()) taxed = (amount * float((1.065))) taxed = str(round(taxed, 2)) print "Taxed Amount Below" print taxed answerwithtip15 = float(taxed) * 1.15 answerwithtip10 = float(taxed) * 1.10 answerwithtip5 = float(taxed) * 1.05 answer15 = raw_input ("Would you like to add a 15% tip?\ny/n\n") main() def main(): if answer15 == "y": print "Your final total is:\n", answerwithtip15 if answer15 == "n": print "Ok" answer10 = raw_input ("Would you like to add a 10% tip?\ny/n\n") else: ("Sorry, We Could not process your request. Please Try Again.") return if answer10 == "y": print "Your final total is:\n", answerwithtip10 if answer10 == "n": print "Ok" answer5 = raw_input ("Would you like to add a 5% tip?\ny/n\n") else: ("Sorry, We Could not process your request. Please Try Again.") return if answer5 == "y": print answerwithtip5 if answer5 == "n": print "Your final total is:\n", taxed else: ("Sorry, We Could not process your request. Please Try Again.") return

I am trying to use a reload command to return back to def main. also idk how to start a def
0
reply
beanboy25
Badges: 2
Rep:
?
#5
Report Thread starter 1 year ago
#5
https://repl.it/repls/NutritiousHelpfulSecurity
0
reply
winterscoming
Badges: 19
Rep:
?
#6
Report 1 year ago
#6
Firstly, when you use the 'def' keyword, you're creating your own custom function. A function is just a block of code which has a name. (as opposed to the code you would write outside of a function which is un-named).

To call a function (i.e. execute the code inside that block), you just use its name and the pair of parenthesis at the end of the name -- you've already been doing this in other places, such as raw_input, which is another function (albeit a function built-in to Python itself).

Also, you've already done that in your example on repl.it with main, and that's working fine:
Code:
main()
reload is another built-in Python function, except the purpose of the 'reload' function is to reload a Python module, but since you haven't got any modules, that's not going to help here.

If you want to repeat the same action more than once, then the simplest thing to do would involve a loop. A loop is a block of code which can keep on repeating over and over. There are different types of loops; the simplest one being 'while'.

The 'while' keyword involves two things:
  1. A block of code to run
  2. A true-or-false check

'while' is a lot like 'if' -- the main difference is that "if" will only perform the check once, and will only execute once. 'while' is basically a repeated-if - it repeats over and over until the check is false.

Example:
Code:
data = raw_input( "Type in the letter 'y': " )
while data != 'y':
  data = raw_input( "You didn't type 'y'.   Type 'y' to stop seeing this message: ";)
Example Output:
Code:
Type in the letter 'y':  n
You didn't type 'y'.   Type 'y' to stop seeing this message:  alkj
You didn't type 'y'.   Type 'y' to stop seeing this message:  jkasdh
You didn't type 'y'.   Type 'y' to stop seeing this message:  y
   >>>

The place you're calling a function from can get data back from that function, just like you'd normally expect from raw_input. The 'return' keyword will let a function send something back to the place the function is called from.

You can take advantage of that fact. Use 'return' from inside a function, and let the 'while' loop decide whether to call the function again:

Code:
# function called 'doSomething()' will return a value
def doSomething():
  result = raw_input("Type 'y': ";)
  # return whatever the user typed in back to the place calling 'doSomething()'
  return result

# put the return-value of 'doSomething()' into the 'data' variable
data = doSomething()
while data != 'y':
  # This will run over and over as long as the 'data' variable isn't 'y'
  print "You didn't type 'y'."
  data = doSomething()
(repl.it) - https://repl.it/repls/AcrobaticJauntyPayware


while loop video: https://www.youtube.com/watch?v=D0Nb2Fs3Q8c
functions video: https://www.youtube.com/watch?v=VhokQhjl5t0

Also try these for some practice:
https://www.codecademy.com/courses/l...sons/functions
https://www.codecademy.com/courses/l.../lessons/loops
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you tempted to change your firm university choice now or on A-level results day?

Yes, I'll try and go to a uni higher up the league tables (131)
19.55%
Yes, there is a uni that I prefer and I'll fit in better (64)
9.55%
No I am happy with my course choice (379)
56.57%
I'm using Clearing when I have my exam results (96)
14.33%

Watched Threads

View All