首页 > 解决方案 > 变量未定义!(Python)

问题描述

我一直在尝试运行我的第一个全文冒险,但每当我运行它时,它都会说答案是未定义的!请帮忙。如果您想知道,这是代码

accept = input("would you like to play the game??")
if accept.lower() .split() == "yes":
    answer: str = input ("you wake up in a room with two doors in front of you. do you go to the left or the right?")
if answer.lower() .split() == "left":
    answer2 = input(" you enter the left door. you find a phone with a peice of paper attached to the wall with the phone\n there are two numbers on the peice of paper\n will you choose to call the first one or the second one")

标签: python

解决方案


原因是条件不满足if语句的要求,所以你从来没有定义答案。是 .split() 弄错了:

accept = input("would you like to play the game??")
if accept.lower() == "yes":
    answer = input ("you wake up in a room with two doors in front of you. do you go to the left or the right?")
    if answer.lower() == "left":
        answer2 = input(" you enter the left door. you find a phone with a peice of paper attached to the wall with the phone\n there are two numbers on the peice of paper\n will you choose to call the first one or the second one")

你看,当你有 str.split() 时,python 将返回一个列表,而不是一个字符串。看看这个:

print("hello".split())

输出:

["hello"]

推荐阅读