【问题标题】:ask again for first input if second input is wrong [duplicate]如果第二个输入错误,再次询问第一个输入[重复]
【发布时间】:2021-12-29 20:45:46
【问题描述】:

假设使用布尔逻辑请求输入一次满足某些条件。 稍后,程序要求另一个输入,但不满足条件。 在这种情况下,程序应该从头开始询问用户第一个输入,即使它是正确的,因为数字 2 输入的条件不正确。

Example:
Give a number that is bigger than 10: 11
Give a number that is smaller than 11: 23
*wrong number, start again*
Give a number that is bigger than 10: 11
Give a number that is smaller than 11: 9
*great*

这只是一个例子,我对思考的过程很感兴趣。到目前为止,如果每个问题中的输入不正确,我只对每个问题再次询问。我需要想办法从第一个问题再问一遍,以防第二个问题作为例子出错。

请不要使用函数,只有循环。我正在学习编程的思维过程。

【问题讨论】:

  • 围绕所有输入进行循环。当任何输入验证失败时,使用continue 回到循环的开头。
  • 根据自己的研究,您尝试过哪些尝试,您的尝试出了什么问题?

标签: python if-statement while-loop


【解决方案1】:
while True:
    num = int(input("Enter number greater than 10: "))
    if num > 10:
        print("Correct.")
        break
    else:
        print("Imcorrect. Try again.")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2021-10-15
    相关资源
    最近更新 更多