python while循环,变量current_question不更新

时间:2016-05-18 16:01:24

标签: python

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

问题 但是,当我测试代码并继续第二个问题时,这个变量不会更新到问题2,3,4但是当我输入错误的数据时,循环只能用第一个问题。我一直试图解决这个问题很长一段时间但仍然没有达成解决方案,所以我认为这将是一个很好的起点。

代码!

SqlCommand command =new SqlCommand("select * from memberform ", con);
con.Open();
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
richTextBox1.Text = (read["mobile"].ToString());
}
read.Close();

2 个答案:

答案 0 :(得分:0)

如果您的意图是针对每个可能的问题进行循环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..

答案 1 :(得分:0)

当问题循环结束时,你的第二次到达 - 由于任何原因造成。在用户键入错误数据后,再次询问问题并结束程序,因为您不会告诉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":
    [...]

等等

相关问题