我正在尝试创建一个RPG,在游戏中玩家可以命名他们的角色。我遇到了一些麻烦,它说这个名字是未定义的。我理解python 2中的raw_input,这不是问题,我测试过,如果我将'input'更改为'raw_input'它根本不起作用,它只会结束程序。而且我想知道是否有任何人可以帮助我理清这个问题。
以下是我的代码(出于隐私目的,我已将我的名字从学分中取出):
import random
PN = 0
Hit = 0
health = 50
level = 0
EXP = 0
nameless_variable_1 = 0
print("Hello, and welcome to my creation!")
print("To begin type 1")
print("to see credits type 2")
print("to quit go to the X button in the corner and click it")
print("to continue type what you want to do and hit enter on your keyboard.")
nameless_variable_1 = input("what is your choice??????")
if nameless_variable_1 == 2:
print("................. The entire game")
print("do you want to play the game now?")
print("if you do press one and then hit enter")
nameless_variable_1 = input(" ")
if nameless_variable_1 == 1:
PN = input("what is your character Name?")
print("it is the year 2019 and you are trying to assasinate an important person")
print("if you fail at this task, you can and probably will die.")
答案 0 :(得分:0)
在python 2中,raw_input
将返回一个字符串,而input
将返回一个计算字符串。因此input
适用于数字,因为eval('2')
给出2
(作为整数)。但是它不适用于名称,因此eval("John")
会引发错误。
我建议您在整个代码中使用raw_input
。
然后,您可以将数字与字符串进行比较
if nameless_variable_1 == "2":
并且对于名称,它不会引发错误。