如何解决“ TypeError:'在<string>'中需要字符串作为左操作数,而不是NoneType”

时间:2019-05-01 22:55:41

标签: python

我正在做一个学校项目,在其中创建了一个带有“指令”屏幕和“标题”屏幕(两者都是不同的.py文件)的游戏,但是我很难在标题屏幕和说明。

我尝试更改您需要按以继续操作的键,并且尝试将标题文件导入到说明文件,反之亦然。

我确实希望当我按下“ 0”时,此功能可以将我带回到“标题”。

from main import titlescreen
global proceed
proceed = None

while proceed not in ("0"): #Line 76
  proceed = input("Press '0' to go back to the tittle.")
  if proceed == "0":
    os.system('clear')
    titlescreen()

ERROR:
Traceback (most recent call last):
  File "main.py", line 113, in <module>
    titlescreen()
  File "main.py", line 92, in titlescreen
    instructions()
  File "/home/runner/no.py", line 76, in instructions
    while pross not in ("0"):
TypeError: 'in <string>' requires string as left operand, not NoneType

1 个答案:

答案 0 :(得分:1)

您要指定proceed = None,然后在两行后尝试查看None是否在字符串中。看起来您正在尝试使其成为字符串元组。如果为proceed提供一个字符串或可以转换为字符串的值,则应该绕过错误

proceed = "2" 

while proceed not in ("0"):
    print("look! I'm printing!", proceed)

    # this part is just to make it so I don't get 
    #an infinite loop without the rest of your code
    proceed = str(int(proceed) - 1)

# ("look! I'm printing!", '2')
# ("look! I'm printing!", '1')

如果要有多个按钮来触发此功能,则在构建它们时将其添加。否则,我建议您检查是否相等,proceed = None

proceed = None 

while not proceed == "0":
    print("look! I'm printing!", proceed)
    proceed = "0"

# ("look! I'm printing!", None)