Home > Blockchain >  What's the value of just pressing "Enter" during an input?
What's the value of just pressing "Enter" during an input?

Time:04-30

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

CodePudding user response:

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

  • Related