6
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.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
Griffin Filmz
  • 101
  • 1
  • 2
  • 5

4 Answers4

22
>>> "{:.1%}".format(0.88)
'88.0%'
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

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)
merlin2011
  • 71,677
  • 44
  • 195
  • 329
2

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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

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.

Community
  • 1
  • 1
Phil Cote
  • 417
  • 1
  • 5
  • 9