【发布时间】:2020-03-22 12:40:30
【问题描述】:
在我的文字冒险游戏中,当我有
你想玩吗(是/否)是
啊!你被什么东西追了!你需要快点!
你应该骑自行车还是滑板(自行车/滑板)滑板
啊!你被什么东西追了!你需要快点!
你应该骑自行车还是滑板(自行车/滑板)
这意味着当我选择一次滑板时,问题会重复,但如果我选择自行车,它会正常进行。这是代码:
def chooseAnswer():
answer=''
while answer.lower().strip() !='yes' and answer.lower().strip() !='no':
answer = input('Would You like To Play (yes/no)')
return answer
def chooseAnswer1():
answer1=''
while answer1.lower().strip() !='bike' and answer1.lower().strip() !='skateboard':
answer1= input('''Ah! You are being chased by something! You need to go fast!\nShould you take the bike or the skateboard (bike/skateboard)''')
return answer1
#Branches off 1 if choose Bike
def chooseAnswer11():
answer11=''
while answer11.lower().strip() !='right' and answer11.lower().strip() != 'left':
answer11= input('''You see two paths. Quickly, you have to decide which path to go on! The left one is dark and looks like it goes down a cave. The right one goes up a hill and into sunlight.(left/right)''')
return answer11
#Branches Off 1 if choose skateboard
def chooseAnswer12():
answer12=''
while answer12.lower().strip() !='sleep' and answer12.lower().strip() != 'awake':
answer12= input('''Quickly you hop on your skateboard, heading for woods.\nYou settle for the night in the woods.\nYou see the mysterious thing that is search for you.\nDo you sleep or stay awake?(awake/sleep)''')
return answer12
if chooseAnswer()=='yes':
if chooseAnswer1()=='bike':
if chooseAnswer11()=='right':
if chooseAnswer111()=='TBD':
print('TBD')
elif chooseAnswer112()=='TBD':
print('TBD')
elif chooseAnswer11=='left':
if chooseAnswer121()=='TBD':
print('TBD')
elif chooseAnswer122()=='TBD':
print('TBD')
elif chooseAnswer1()=='skateboard':
if chooseAnswer12()=='awake':
有人知道为什么输入提示重复两次吗?
【问题讨论】:
-
您的示例代码不完整。至少,我们希望看到您的 chooseAnswer* 函数的定义。
-
您的代码很难阅读,因此也很难维护。我建议你写一个框架来读取用户输入有一个通用的方法来处理输入
标签: python function if-statement logic