【问题标题】:Why the user input still go through the loop while I'm entering "YES" to the RobotQ1 input?为什么当我在 RobotQ1 输入中输入“YES”时,用户输入仍然通过循环?
【发布时间】:2020-04-23 17:59:14
【问题描述】:

基本上,一旦用户决定根据 RobotQ1 变量输入“YES”,我希望代码重新开始,但是,它会通过 RobotQ2 的第二个用户输入。

restart = "Yes".lower()
while restart == "YES".lower():
         print(" In order to obtain the harmonized code of your item answer all the following questions")
         print()
         print("Q1 : Does your car have 4 wheel drive")
         RobotQ1 = input("YES / NO ?:")
         if RobotQ1 == "YES".lower():
            print("Here is the HS code:...")
            restart = input("Do you want to use the application? (Yes / No): ")         
         if restart =="NO".lower():
            print("bye")
            break
         elif RobotQ1 == "NO".lower():
            print("Q2 : Does your Robot have 7 axes ?")
         RobotQ2 = input("YES / NO ?:")   
         if RobotQ2 == "YES".lower():
            print("Here is the HS code:....")
            restart = input("Do you want to use the application? (Yes / No): ")
         if restart =="NO".lower():
            print("bye")
            break

【问题讨论】:

  • 你不也想小写RobotQ1:if RobotQ1.lower() == "yes":。因为否则,"YES" != "yes"

标签: python python-3.x loops input while-loop


【解决方案1】:

基本上你想要的是

如果 RobotQ1 为 Yes==> 提供 HS 代码并要求重新启动

如果机器人 Q1 为否 ==> 询问 7 轴(如果是)==> 给出 Hs 代码 ==> 要求重新统计

我觉得这里不错

restart = "Yes".lower()

while restart == "Yes".lower():
    print(" In order to obtain the harmonized code of your item answer all the following questions")
    print()
    print("Q1 : Does your car have 4 wheel drive")
    RobotQ1 = input("Yes/No ?:")

    if RobotQ1.lower() == "Yes".lower():
        print("Here is the HS Code ...")
    else:
        print("Q2 : Does your Robot have 7 axes ?")
        RobotQ2 = input("Yes/No")
        if RobotQ2.lower() == "Yes".lower():
            print("Here is Hs Code")

    restart = input("Do you want to use the application? (Yes / No): ")
    restart.lower()

    if restart == "No".lower():
        print("bye")
        break

【讨论】:

    【解决方案2】:

    所以你想在不中断的情况下重新开始你的循环,在这种情况下:

    添加continue

    Python 中的 continue 语句将控件返回到开头 的当前循环。遇到时,循环开始下一次迭代 不执行当前迭代中的剩余语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2021-01-19
      • 2021-04-22
      • 2021-06-17
      • 2022-01-20
      相关资源
      最近更新 更多