I’m trying to write a code in Python where you ask for input for a price, and then ask for input of the % of taxes. Then, you print the final price (price plus taxes). The code works fine, however I’m missing something.
I also need to make so if the user just presses enter during the input of the taxes, the taxes value goes default to 21%, but how do I do that? I’ve tried like if taxes == None, and things like that but it doesn’t work.
My code is like this:
def calc():
price = float(input("Insert price without taxes: "))
taxes = float(input("Insert percentage of taxes: "))
tax2 = taxes / 100
tax3 = price * tax2
total = price + tax3
print ("Total price including taxes is: ", total)
calc()
But I’m still missing the part of having the taxes value go to a default 21% when the input is just the press of the Enter key and nothing else. How do I do that? Thank you
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
try
def calc():
price = float(input("Insert price without taxes: "))
taxes = float(input("Insert percentage of taxes: ") or 21)
tax2 = taxes / 100
tax3 = price * tax2
total = price + tax3
print ("Total price including taxes is: ", total)
calc()
simple ‘or’ works here
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0