Feb-07-2018, 09:53 PM
(This post was last modified: Feb-07-2018, 09:53 PM by Gribouillis.)
I made very small changes in your code so that it now works
num = float(input('Enter the cost of your meal: ')) def conver(num): while num < 0 or num == 0: num = float(input('please enter a positive number: ')) #rejects numbers 0 or below if num > .01 and num < 5.99: tip = (num / 100) * 10 elif num > 6 and num < 12: tip = (num/100) * 15 elif num > 12.01 and num < 17: tip = (num/100) * 20 elif num > 17.01 and num < 25: tip = num/100 * 25 else: tip = (num/100) * 30 return tip #calculates tip depending on input def calculate(num, tip): tax = (num / 100) * 10 total = num + tip + tax return total, tax tip = conver(num) total, tax = calculate(num, tip) print("Food cost: $", round(num)) print("Tip: $",'{0:.1f}'.format(tip)) print("Tax: $",'{0:.1f}'.format(tax)) print("Total: $",'{0:.1f}'.format(total)) #Displays results back to user after calculations. # The tax and total are set to one digit after the decimal, float values as shown. #The example showed integer values for 'food' and 'tip' so they were rounded.Tips:
- Indent python code with 4 spaces everywhere. You can configure your editor to insert 4 space characters when you hit the tab key.
- The value of variables can be used only after variables have been initialized. After
spam = ...
you can print spam.
- You strongly prefer C and C++ but very soon you will strongly prefer python because it will give you easily results that C and C++ can only give after a tremendous programming effort.
- Read carefully python's error message. They contain a lot of information about your error, such as the line where the error occurred.