根据用户的输入打印正确的事件
Print the correct event, depending on user's input
我正在开发一款基于文本的冒险游戏。本质上,我想 return 正确的事件取决于用户类型。现在,无论用户键入什么,它都会给出相同的事件(EVENT_DRAGON 每次都是 returned)。在游戏的大部分时间里,用户可以在 1、2 或 3 之间进行选择。哪个工作得很好,但我想将其切换并要求用户输入单词。这一直没有正常工作.. 澄清为什么它可以使用编号输入而不是单词输入将不胜感激。谢谢。
def main():
import sys
def run_event(event):
text, choices = event
text_lines = text.split("\n")
for line in text_lines:
print('' + line)
print("")
choices = choices.strip("\n")
choices_lines = choices.split("\n")
for num, line in enumerate(choices_lines):
print('' + line)
print("")
print ("")
print ("You have found yourself stuck within a dark room, inside this room are 5 doors.. Your only way out..")
print ("")
print ("Do you want to enter door 1,2,3,4, or 5?")
print ("")
EVENT_DOOR3 = ("""
A dragon awaits you
""","""
'Welcome' remarks the dragon. 'Do you wish to escape?""")
EVENT_DRAGON = ("""
'Good choice' says the dragon. Where would you like to escape to?
""","""
1. Just get me out of here!
2. Where should I escape to?
""")
EVENT_DRAGON2 = ("""
Interesting..
""","""
Test..
""")
door = input("> ")
if door == "3":
run_event(EVENT_DOOR3)
dragon = input()
if dragon in ['yes','Yes']:
run_event(EVENT_DRAGON)
elif dragon in ['no','No']:
run_event(EVENT_DRAGON2)
main()
这一行会给您带来一些麻烦,因为它的计算结果总是 True
。
if dragon == "yes" or "Yes":
run_event(EVENT_DRAGON)
这个条件类似于说:
if (dragon == 'yes') or ('Yes'):
run_event(EVENT_DRAGON)
因为 'Yes' 是一个非空字符串,它将计算为 True
,并且它总是 run_event(EVENT_DRAGON)
。有几种不同的方法可以解决这个问题。首先,您可以将输入更改为小写以仅评估一个单词:
if dragon.lower() == 'yes':
run_event(EVENT_DRAGON)
此外,您可以将可接受的单词放入列表中:
if dragon in ['Yes', 'yes']:
run_event(EVENT_DRAGON)
您也可以单独测试每个单词:
if dragon == 'yes' or dragon == 'Yes':
run_event(EVENT_DRAGON)
希望对您有所帮助。如果这不起作用,请告诉我。
我正在开发一款基于文本的冒险游戏。本质上,我想 return 正确的事件取决于用户类型。现在,无论用户键入什么,它都会给出相同的事件(EVENT_DRAGON 每次都是 returned)。在游戏的大部分时间里,用户可以在 1、2 或 3 之间进行选择。哪个工作得很好,但我想将其切换并要求用户输入单词。这一直没有正常工作.. 澄清为什么它可以使用编号输入而不是单词输入将不胜感激。谢谢。
def main():
import sys
def run_event(event):
text, choices = event
text_lines = text.split("\n")
for line in text_lines:
print('' + line)
print("")
choices = choices.strip("\n")
choices_lines = choices.split("\n")
for num, line in enumerate(choices_lines):
print('' + line)
print("")
print ("")
print ("You have found yourself stuck within a dark room, inside this room are 5 doors.. Your only way out..")
print ("")
print ("Do you want to enter door 1,2,3,4, or 5?")
print ("")
EVENT_DOOR3 = ("""
A dragon awaits you
""","""
'Welcome' remarks the dragon. 'Do you wish to escape?""")
EVENT_DRAGON = ("""
'Good choice' says the dragon. Where would you like to escape to?
""","""
1. Just get me out of here!
2. Where should I escape to?
""")
EVENT_DRAGON2 = ("""
Interesting..
""","""
Test..
""")
door = input("> ")
if door == "3":
run_event(EVENT_DOOR3)
dragon = input()
if dragon in ['yes','Yes']:
run_event(EVENT_DRAGON)
elif dragon in ['no','No']:
run_event(EVENT_DRAGON2)
main()
这一行会给您带来一些麻烦,因为它的计算结果总是 True
。
if dragon == "yes" or "Yes":
run_event(EVENT_DRAGON)
这个条件类似于说:
if (dragon == 'yes') or ('Yes'):
run_event(EVENT_DRAGON)
因为 'Yes' 是一个非空字符串,它将计算为 True
,并且它总是 run_event(EVENT_DRAGON)
。有几种不同的方法可以解决这个问题。首先,您可以将输入更改为小写以仅评估一个单词:
if dragon.lower() == 'yes':
run_event(EVENT_DRAGON)
此外,您可以将可接受的单词放入列表中:
if dragon in ['Yes', 'yes']:
run_event(EVENT_DRAGON)
您也可以单独测试每个单词:
if dragon == 'yes' or dragon == 'Yes':
run_event(EVENT_DRAGON)
希望对您有所帮助。如果这不起作用,请告诉我。