【问题标题】:In python, my input prompt is printing a second time after I answer it once在python中,我的输入提示在我回答一次后第二次打印
【发布时间】: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


【解决方案1】:

问题是由在条件句中调用 chooseAnswer1 函数引起的。

if chooseAnswer()=='yes':
    # First call of chooseAnswer1
    if chooseAnswer1()=='bike':

        ...Code Removed for Readability...

    # second call of chooseAnswer1 
    elif chooseAnswer1()=='skateboard':
        if chooseAnswer12()=='awake':

要解决此问题,您需要将函数调用分配给一个变量,然后在条件中将它返回的对象与该变量进行比较。您可能希望对整个程序的所有函数都执行此操作:

# Single call of chooseAnswer
answer0 = chooseAnswer()
if answer0 == 'yes':
    # Single call of chooseAnswer1
    answer1 = chooseAnswer1()
    if answer1 == 'bike':

        ...Code Removed for Readability...

    # Since the variable is assigned to the returned object,
    # the function is not called again in the elif condition.
    elif answer1 == 'skateboard':
        answer12 = chooseAnswer12()
        if answer12 == 'awake':


Python 3.8 的新 walrus := 运算符在这里也可能有用,但我仍在使用 3.7,所以我将把它留给其他人提供代码示例,说明如何在这种情况下使用它.

编辑:OP 在对我的回答的评论中说:

所以我遇到了另一个问题。我分配了所有函数变量,所以当我认为它按顺序排列时,我将变量放入。我的意思是当第一个问题弹出时,它回答它就好像我说是的,即使我说滑板,它给我自行车道。

由于此 python tutor link 的 URL 有多长,我的回复太长而无法发布到评论中,但我建议他们将其函数调用的位置与我在示例中放置的位置进行比较:

answer0 = chooseAnswer()
if answer0 == 'yes':
    answer1 = chooseAnswer1()
    if answer1 == 'bike':
        answer11 = chooseAnswer11()
        if answer11 == 'right':
            print("right path")
        elif answer11 == 'left':
            print("left path")
    elif answer1 == 'skateboard':
        answer12 = chooseAnswer12()
        if answer12 == 'sleep':
            print("sleep path")
        elif answer12 == 'awake':
            print("awake path")

这里的关键细节是函数调用的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多