首页 > 解决方案 > 如何修复“TypeError:*:'NoneType'和'float'不支持的操作数类型”?

问题描述

我必须创建这个程序,让用户输入他们的年龄以及他们是否有折扣。然后我必须使用if/elif/else语句来输出年龄范围和折扣价等。

我不知道我做错了什么。它说

TypeError: *: 'NoneType' 和 '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.")

标签: pythontypeerror

解决方案


你有几行这样的,你期望print()返回一些东西:

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

但是,这不起作用,因为print()不返回任何东西,price所以None. 有关为什么不返回任何内容的信息,请参见此处。print()

目前尚不清楚您期望从中返回什么print;似乎您想price成为一个数字,例如18.99,因为您将其乘以某物。在这种情况下,您必须为其分配一个值,并将打印放在自己的行中:

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

如果要打印价格,可以使用f 字符串在打印文本中包含价格值:

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

无论您选择哪种选项,您都必须为priceNprice2和同一问题的所有其他实例执行此操作。


推荐阅读