基于文本的游戏输入



我可能只是愚蠢,但我真的想不通。我正在制作一个基于文本的游戏,为了模拟不同场景中的角色移动,我使用了以下代码:

 import random
 north = ("There is a path to the North. ")
 south = ("There is a path to the South. ") 
 east  = ("There is a path to the East. ")
 west = ("There is a path to the West. ")
 nsew = [north, south, east, west]
 print ("You find yourself in a woodland clearing. ")
 print (random.choice(nsew))
 print (random.choice(nsew))
 print (random.choice(nsew))
 print (random.choice(nsew))
 direction = input("Which way do you go? ")

这应该从列表中打印四个随机项目,它确实如此。我希望能够执行以下操作:

if direction == ("North"):
    print ("You decide to go North.")

但是每个都有一个 if 语句,因此如果用户键入"东",程序将响应"你决定去东"

谢谢

import random
paths = []
tiaptt = "There is a path to the "
nsew = ["north", "south", "east", "west"]
amount = random.randrange(1,4)

for path in random.sample(nsew,amount):
    print tiaptt+path
    paths.append(path.lower())
direction = raw_input("Which way do you go? ")
try:
    index = paths.index(direction.lower())
    print "You have chosen to go %s"%paths[index]
except:
    print "You have chosen a wrong direction"

希望这有帮助。它适用于python 2.7,如果您使用3+,则可能需要将我的raw_input更改为输入。并在印刷品周围加上 ()。

print ("You have chosen to go %s"%paths[index])

好吧,您首先读取用户输入,将所有字母设置为小写,然后将结果字符串与"北"、"南"、"东"和"西"进行比较。

相关内容

  • 没有找到相关文章

最新更新