Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python modules?
#3
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.
Reply


Messages In This Thread
python modules? - by Kuron3ko - Feb-07-2018, 09:19 PM
RE: python modules? - by nilamo - Feb-07-2018, 09:47 PM
RE: python modules? - by Gribouillis - Feb-07-2018, 09:53 PM
RE: python modules? - by Kuron3ko - Feb-07-2018, 10:15 PM
RE: python modules? - by buran - Feb-08-2018, 07:14 AM
RE: python modules? - by kmcollins - Feb-10-2018, 04:47 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020