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
68.7k41 gold badges188 silver badges311 bronze badges
asked Jan 25, 2015 at 23:11
2
>>> "{:.1%}".format[0.88]
'88.0%'
answered Sep 22, 2015 at 23:49
jfsjfs
382k180 gold badges946 silver badges1617 bronze badges
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]
answered Jan 25, 2015 at 23:16
merlin2011merlin2011
68.7k41 gold badges188 silver badges311 bronze badges
4
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.
answered Jan 25, 2015 at 23:21
0
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.
answered Jan 25, 2015 at 23:48
Phil CotePhil Cote
3971 gold badge4 silver badges9 bronze badges
Calculate percentage from user Input in Python #
To calculate percentage from user input:
- Convert the input values to floats.
- Use the division
/
operator to divide one number by another. - Multiply the quotient by
100
to get the percentage. - The result shows what percent the first number is of the second.
Copied!
# 👇️ if you need to format an input value to 1 or more decimal places user_input = input['Type a percentage, e.g. 10: '] result = f'{float[user_input] / 100:.1%}' print[result] # 👉️ 10.0% # ---------------------------------------- # 👇️ make sure to convert input[] result to float[] def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 print[is_what_percent_of[25, 75]] # 👉️ 33.33 print[is_what_percent_of[15, 93]] # 👉️ 16.12903.. print[round[is_what_percent_of[15, 93], 2]] # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0 # -------------------------------------------------- def get_remainder[num_a, num_b]: return num_a % num_b print[get_remainder[50, 15]] # 👉️ 5 print[get_remainder[50, 20]] # 👉️ 10
The first example uses a formatted string literal to format an input value to 1 or more decimal places.
Copied!
user_input = input['Type a percentage, e.g. 10: '] result = f'{float[user_input] / 100:.1%}' print[result] # 👉️ 10.0% result = f'{float[user_input] / 100:.2%}' print[result] # 👉️ 10.00%
Formatted string literals [f-strings] let us include expressions inside of a string by prefixing the string with f
.
Make sure to wrap expressions in curly braces - {expression}
.
We are also able to use the format specification mini-language in expressions in f-strings.
Copied!
my_float = 0.79 result_1 = f'{my_float:.1%}' print[result_1] # 👉️ 79.0% result_2 = f'{my_float:.2%}' print[result_2] # 👉️ 79.00%
If you need to calculate what percent one number is of another, divide one number by the other and multiply the result by 100
.
Copied!
def is_what_percent_of[num_a, num_b]: return [num_a / num_b] * 100 num1 = float[input['Enter a number: ']] # 👉️ 25 num2 = float[input['Enter another number: ']] # 👉️ 75 result = is_what_percent_of[num1, num2] print[result] # 👉️ 33.33333333333333 print[round[result, 2]] # 👉️ 33.33
Make sure to use the float[]
class to convert the input strings to floating-point numbers.
The
input[]
function is guaranteed to return a string even if the user enters a number.
Use the round[]
function if you need to round the result to N decimal places.
The round
function returns the number rounded to ndigits
precision after the decimal point.
Note that if you try to divide by 0
, you'd get a ZeroDivisionError
.
If you need to get the percentage increase from one number to another, use the following function.
Copied!
def get_percentage_increase[num_a, num_b]: return [[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ -60.0
The first example shows that the percentage increase from 60
to 30
is 100 %
.
And the second example shows that the percentage increase from 40
to 100
is -60%
.
If you always need to get a positive number, use the abs[]
function.
Copied!
def get_percentage_increase[num_a, num_b]: return abs[[num_a - num_b] / num_b] * 100 print[get_percentage_increase[60, 30]] # 👉️ 100.0 print[get_percentage_increase[40, 100]] # 👉️ 60.0
The abs function returns the absolute value of a number. In other words, if the number is positive, the number is returned, and if the number is negative, the negation of the number is returned.