Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn input number into a percentage 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 have written this but I do not know how to get

print(tip)

to be a percentage when someone types a number in.

like image 255
Griffin Filmz Avatar asked Jan 25 '15 23:01

Griffin Filmz


3 Answers

>>> "{:.1%}".format(0.88)
'88.0%'
like image 163
jfs Avatar answered Nov 18 '22 17:11

jfs


Based on your usage of input() rather than raw_input(), I assume you are using python3.

You just need to convert the user input into 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)
like image 2
merlin2011 Avatar answered Nov 18 '22 17:11

merlin2011


It will be

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

The end %% prints the percent sign. The number (100*float(tip)/meal) is what you are looking for.

like image 2
Corrupted MyStack Avatar answered Nov 18 '22 17:11

Corrupted MyStack