当用户输入“ East”时,我希望输出为-1而不是“ East”
East = -1
Xdirectioninput = float(input("Is the player South or East: "))
Xdirectioninput = (Xdirectioninput)
print (Xdirectioninput)
答案 0 :(得分:1)
这仅在Python 2中有效。之所以起作用,是因为在Python 2中,用户为响应input()
而键入的内容被评估为Python表达式。
但是您无法在Python 3中做到这一点。一种实现方法是设置带有方向的字典:
directions = {"East": -1.0, "South": -2.0}
Xdirectioninput = directions[input("Is the player South or East: ")]
答案 1 :(得分:0)
我认为条件语句将是一个很好的解决方案。您可以编写如下代码:
Xdirectioninput = input("Is the player South or East: ")
if Xdirectioninput == 'East':
Xdirection = -1
print(Xdirection)