简体   繁体   中英

How to fix “TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'”?

I have to create this program that lets the user input their age and if they have a discount. I then have to use if/elif/else statements to output the age range and discount price etc.

I don't know what I'm doing wrong. It says

TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

#1. Input (Age and discount)
print("Welcome to the Toronto Zoo!")
age = int(input("Please input your age: "))
coupon = input("Do you have a coupon? (Y or N): ")

repeat = "Y"
#2. Process (Calculate age group, discount(if applicable), HST)
while repeat == "Y" :
    if age<=17 and coupon == "Y": #If the user is under 18 and has a coupon, this code block will run
        print("Your Age Category: Child")
        price = print("Your Ticket price: $18.99")
        hst= round(price*0.13, 2)
        final = round(price-hst, 2)
        discount= round(final*0.10, 2)
        coupon= round(final-discount, 2)
        print("Your discount is: $", discount)
        print("Your final cost, including HST sales tax, will be: $", coupon)
    elif age<=17 and coupon == "N" : #If the user is under 18 and doesn't have a coupon, this code block will run
        print("Your Age Category: Child")
        priceN = print("Your Ticket price: $18.99")
        hstN= round(priceN*0.13, 2)
        finalN = round(priceN-hstN, 2)
        print("Your final cost, including HST sales tax, will be: $", finalN)
    elif age==18 or age<=64 and coupon== "Y": #If the user is aged from 18-64 and has a coupon, this code block will run
        print("Your Age Category: Adult")
        price2 = print("Your Ticket price: $28.99")
        hst2= round(price2*0.13, 2)
        final2 = round(price2-hst2, 2)
        discount2= round(final2*0.10, 2)
        coupon2= round(final2-discount2, 2)
        print("Your discount is: $", discount2)
        print("Your final cost, including HST sales tax, will be: $", coupon2)
    elif age==18 or age<=64 and coupon== "N": #If the user is aged from 18-64 and doesn't have a coupon, this code block will run
        print("Your Age Category: Adult")
        priceN2 = print("Your Ticket price: $28.99")
        hstN2= round(priceN2*0.13, 2)
        finalN2 = round(priceN2-hstN2, 2)
        print("Your final cost, including HST sales tax, will be: $", finalN2)
    elif age>64 and coupon== "Y": #If the user is above 64 years old and has a coupon, this code block will run
        print("Your Age Category: Senior")
        price3 = print("Your Ticket price: $23.99")
        hst3= round(price3*0.13, 2)
        final3 = round(price3-hst3, 2)
        discount3= round(final3*0.10, 2)
        coupon3= round(final3-discount3, 2)
        print("Your discount is: $", discount3)
        print("Your final cost, including HST sales tax, will be: $", coupon3)
    elif age>64 and coupon== "N": #If the user is above 64 years old and doesn't have a coupon, this code block will run
        print("Your Age Category: Senior")
        price = print("Your Ticket price: $23.99")
        priceN3 = print("Your Ticket price: $28.99")
        hstN3= round(priceN3*0.13, 2)
        finalN3 = round(priceN2-hstN3, 2)
        print("Your final cost, including HST sales tax, will be: $", finalN3)
    else:
        print("This is invalid.")

repeat= input("Would you like to repeat this program again? Enter Y or N: ")
print("Thank you for using the Ticket Cost Program.")

You have several lines like this, where you're expecting print() to return something:

price = print("Your Ticket price: $18.99")
hst= round(price*0.13, 2)

This, however, won't work, because print() doesn't return anything, so price will be None . See here for info on why print() doesn't return anything.

It's not clear what you expect to be returned from print ; it seems that you want price to be a number, like 18.99 , since you're multiplying it by something. In that case, you'd have to assign it a value, and put the print in its own line:

print("Your Ticket price: $18.99")
price = 18.99
hst= round(price*0.13, 2)

If you wanted to print out the price, you could use an f string to include the price value in the printed text:

price = 18.99
print(f"Your Ticket price: ${round(price, 2)}")
hst= round(price*0.13, 2)

Regardless of the option you choose, you'd have to also do this for priceN , price2 , and all other instances of this same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM