【问题标题】:python while loop, variable current_question is not updatingpython while循环,变量current_question没有更新
【发布时间】:2016-05-19 00:19:06
【问题描述】:

我的目标是: 我目前正在尝试构建一个简单的故障排除程序。变量 current_question 旨在更新为嵌套 if 语句中每个输入/问题的值。这样代码就可以识别它当前正在询问的问题。因此,如果用户输入了错误的数据(不是是和不是),我可以循环回到当前问题

问题 但是,当我测试代码并继续到第二个问题时,此变量不会更新为问题 2、3、4 但每当我输入错误数据时,循环仅适用于第一个问题。我一直在尝试解决这个问题,但仍然没有找到解决方案,所以我认为这是一个很好的起点。

代码!

import time
current_question = ()
i = 1
while i <= 4:
    q1 = str(input('Is your phone wet? ')).lower()
    current_question = q1
    i = i + 1
    if q1 == 'yes' or q1 == 'Yes':
        print('dry it out')
        break
    elif q1 == 'no' or q1 == 'No':
        q2 = str(input('is your phone cracked? ')).lower()
        current_question = q2
        i = i + 1
        if q2 == 'yes' or q2 == 'Yes':
            print('replace screen')
            break
        elif q2 == 'no' or q2 == 'No':
            q3 = str(input('are you able to download apps/videos? ')).lower()
            current_question = q3
            i = i + 1
            if q3 == 'yes' or q3 == 'Yes':
                print('delete any apps or videos that you don\'t need')
                break
            elif q3 == 'no' or q3 == 'No':
                q4 = str(input('Is your phone unresponsive? ')).lower()
                current_question = q4
                i = i + 1
                if q4 == 'yes' or q4 ==  'Yes':
                    print('restart it')
                    break
                elif q4 == 'no' or q4 == 'No':
                    print('contact the supplier')
                    break
while current_question != 'yes' and current_question != 'no':
    print('try again')
    time.sleep(1)
    print(current_question)

【问题讨论】:

  • 您将current_question 变量设置为用户给您的响应,这是您的意思吗?
  • 同样在你最后一个while循环中你永远不会改变current_question的值,所以它是一个无限循环
  • 您可能正在寻找一种方法来跟踪您在with something like this上的哪个问题
  • 只是想知道为什么 current_question 默认为元组而不是字符串?

标签: python


【解决方案1】:

当问题循环结束时,您的第二个 while 到达 - 由任何原因引起。用户输入错误数据后,再次提问,程序结束,因为你没有告诉python恢复主循环。

摆脱当前的循环 ans 而不是 ifs 使用你的循环:

q1 = str(input('Is your phone wet? ')).lower()
while q1 != "yes" and q1 != "Yes" and q1 != "no" and q1 != "No":
    q1 = str(input('Is your phone wet? ')).lower()
if q1 == 'yes' or q1 == 'Yes':
    print('dry it out')
elif q1 == 'no' or q1 == 'No':
    q2 = str(input('is your phone cracked? ')).lower()
    while q2 != "yes" and q2 != "Yes" and q2 != "no" and q2 != "No":
    [...]

等等

【讨论】:

  • 谢谢大家的帮助!我明白了,非常感谢:)
【解决方案2】:

如果您的意图是针对每个可能的问题进行循环,if-elif 将不会这样做。对于每种可能的情况,您必须使用单独的 if,并且必须删除 break 语句。

if q1 == 'yes' or q1 == 'Yes':
        print('dry it out')
if q1 == 'no' or q1 == 'No':
        q2 = str(input('is your phone cracked? ')).lower()
        current_question = q2
        i = i + 1
if q2 == 'yes' or q2 == 'Yes':
        print('replace screen')
if q2 == 'no' or q2 == 'No':
            q3 = str(input('are you able to download apps/videos? ')).lower()
            current_question = q3
            i = i + 1
etc..

【讨论】:

  • 我建议使用if q1 in ('yes', 'Yes'): 甚至if q1.lower() == 'yes':
猜你喜欢
  • 2019-03-05
  • 1970-01-01
  • 2018-06-10
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多