How to enable percentage input in python

print ("How much does your meal cost")

meal = 0
tip = 0
tax = 0.0675

action = input( "Type amount of meal ")

if action.isdigit():
    meal = (action)
    print (meal)

tips = input(" type the perentage of tip you want to give ")


if tips.isdigit():
    tip = tips 
    print(tip)

I wrote this, but I do not know how to get

print(tip)

- percentage when someone enters a number.

+4
source share
4 answers
>>> "{:.1%}".format(0.88)
'88.0%'
+13
source

Based on your use input(), and not raw_input(), I assume that you are using python3.

You just need to convert the user input to a floating point number and divide by 100.

print ("How much does your meal cost")

meal = 0
tip = 0
tax = 0.0675

action = input( "Type amount of meal ")

if action.isdigit():
    meal = float(action)

tips = input(" type the perentage of tip you want to give ")

if tips.isdigit():
    tip = float(tips)  / 100 * meal
    print(tip)
+2
source

print "Tip = %.2f%%" % (100*float(tip)/meal)

%% . (100*float(tip)/meal) - , .

+1

, , . , , . . , .155 15.5 15,5%. run-of-the-mill if. ( float)

if tip > 1:
    tip = tip / 100

, . :

tip = (tip / 100) if tip > 1 else tip

, .

+1

All Articles