python - 变量未定义!(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")
解决方案
原因是条件不满足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"]
推荐阅读
- angular - Nswag Typescript API 客户端 InjectionToken 没有被解析
- reactjs - 如何使用新的 auth0-spa.js 在不单击登录按钮的情况下重定向到 auth0?
- jenkins - jenkinsfile 页面加载事件
- javascript - 为什么我的 Ajax 调用没有超过 readystate 1?
- r - 如何计算R中数据框中每行出现多个字符串的次数
- jestjs - Jest 在测试期间没有显示绿色进度条
- python - TypeError:“模块”对象在 Python 3 中不可调用
- c++ - 在 C++ 中使用共享内存时出现 SIGSEGV 错误
- html - 页脚高度不固定的问题
- shell - 循环并组合两个文本文件的脚本