Reputation: 101
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.
Upvotes: 6
Views: 53792
Reputation: 417
We're assuming that it's a number the user is putting in. We want to make sure that the number is a valid percentage that the program can work with. I would recommend anticipating both expressions of a percentage from the user. So the user might type in .155
or 15.5
to represent 15.5%. A run-of-the-mill if statement is one way to see to that. (assuming you've already converted to float)
if tip > 1:
tip = tip / 100
Alternatively, you could use what's called a ternary expression to handle this case. In your case, it would look something like this:
tip = (tip / 100) if tip > 1 else tip
There's another question here you could check out to find out more about ternary syntax.
Upvotes: 1
Reputation: 451
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.
Upvotes: 2
Reputation: 75575
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)
Upvotes: 2