【发布时间】:2022-12-24 13:09:52
【问题描述】:
print("Welcome to the rollercoaster!")
height_input = int(input("What is your height in cm? "))
height_input >= 120
if height_input >= 120:
print("Congrats! You're allowed to go on ride.")
else: print("Sorry, you're not allowed on ride.")
if height_input >= 120:
payment = (int(input("How old are you? ")))
if payment <= 18:
print("You must pay $7")
else: print("You must pay $12")
(如果我将高度设置为小于 120cm,当我将高度设置为 120+ 时会出现错误代码运行顺利)提前抱歉我是编码新手
我收到错误
Traceback (most recent call last):
File "main.py", line 14, in <module>
if payment <= 18:
NameError: name 'payment' is not defined
【问题讨论】:
-
payment可以未定义,因为它在if语句中。 -
这类错误最好先为你的程序画一个图来解决。问问自己:如果
height < 120,你想让程序继续吗?如果不是,您应该如何修改代码以反映这一点? -
height_input >= 120..?在第三行 -
@YashMehta 这只是多余的,但不会造成伤害。
标签: python