Python 名称错误:名称';q2和x27;没有定义

Python 名称错误:名称';q2和x27;没有定义,python,python-3.x,defined,Python,Python 3.x,Defined,我昨天刚开始使用Python3,遇到了这个错误,我不知道如何修复,请有人帮忙。谢谢 编辑:这不是缩进,很抱歉,当我将代码粘贴到这里时,这里的代码可能以错误的缩进结束,并且必须使用4个空格 q1 = input("Is it currently raining? ") if q1 == "Yes": print("You should take the bus.") elif q1 == "No": q2=int(input('How far in km do you need t

我昨天刚开始使用Python3,遇到了这个错误,我不知道如何修复,请有人帮忙。谢谢 编辑:这不是缩进,很抱歉,当我将代码粘贴到这里时,这里的代码可能以错误的缩进结束,并且必须使用4个空格

q1 = input("Is it currently raining? ")
if q1 == "Yes":
    print("You should take the bus.")
elif q1 == "No":
    q2=int(input('How far in km do you need to travel? '))

if q2 > 10:
    print("You should take the bus.")
elif q2 >= 2 and q2 <= 10:
    print("You should ride your bike.")
elif q2 == 1:
    print("You should walk.")
q1=input(“当前正在下雨吗?”)
如果q1==“是”:
打印(“你应该坐公共汽车。”)
elif q1==“否”:
q2=int(输入(“您需要行驶多远,以公里为单位?”)
如果q2>10:
打印(“你应该坐公共汽车。”)
elif q2>=2和q2 10:
NameError:未定义名称“q2”

缩进表示正在检查
q2
,即使
q1==“Yes”
,请尝试:

q1 = input("Is it currently raining? ")
if q1.lower() in ("yes", "y"): # more flexible input
    print("You should take the bus.")
elif q1 == "No":
    q2 = int(input('How far in km do you need to travel? ')) 
    if q2 > 10: # inside elif block
        print("You should take the bus.")
    elif q2 >= 2: # already know it's 10 or under
        print("You should ride your bike.")
    elif q2 == 1:
        print("You should walk.")

请检查缩进。您确定正在运行Python3吗,看起来您正在2上进行测试。您可以尝试将输入更改为原始输入,因为输入在2.x和3I中具有不同的含义,我尝试使用原始输入,但Python3中不存在
q1 = input("Is it currently raining? ")
if q1.lower() in ("yes", "y"): # more flexible input
    print("You should take the bus.")
elif q1 == "No":
    q2 = int(input('How far in km do you need to travel? ')) 
    if q2 > 10: # inside elif block
        print("You should take the bus.")
    elif q2 >= 2: # already know it's 10 or under
        print("You should ride your bike.")
    elif q2 == 1:
        print("You should walk.")