【问题标题】:Prevent command from running阻止命令运行
【发布时间】:2017-04-20 17:08:16
【问题描述】:

如果用户在 print(M1) 中输入“否”,我将如何停止运行最后一个打印语句?我希望程序在打印 M1 时完成。我得到错误 由于在 if 块中声明了“TaxCode”,当用户输入“否”时出现“NameError:未定义名称“TaxCode”。 谢谢。

name = input("What is your name? ")
while True:
    try:
        income = int(input("What is your income? ($) "))
        break
    except ValueError:
        print ("Invalid input\nPlease enter a figure")
        continue
    else:
        break

M1  = "This program cannot determine your tax code. Please use the program for secondary income "

print("Please answer the questions with 'yes' and 'no'")


Q1=input("Do you receive an income tested benefit? ")
while Q1 != "yes" and Q1 != "no":
    print("\nPlease enter either 'yes' or 'no'")
    Q1=input("Do you recieve an income tested benefit? ")
    if Q1=="yes":
                 Q2=input("Is this tax code for the income tested benefit? ")
                 while Q2 != "yes" and Q2 != "no":
                     print("\nPlease enter either 'yes' or 'no'")
                     Q2=input("Is this tax code for the income tested benefit? ")
                      if Q2=="yes":
                                TaxCode = "M"
                      elif Q2=="no":
                                  print (M1)

print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)

【问题讨论】:

    标签: python printing terminate


    【解决方案1】:

    在 if 语句之前定义 TaxCode 为空值,以防止 NameError。这样TaxCode =''

    你可以使用exit(0)来终止程序,这里()里面的数字代表成功/失败。与 linux 中的退出状态相同。所以你的代码将是

    while Q1 != "yes" and Q1 != "no":
        print("\nPlease enter either 'yes' or 'no'")
        Q1=input("Do you recieve an income tested benefit? ")
        if Q1=="yes":
            Q2=input("Is this tax code for the income tested benefit? ")
             while Q2 != "yes" and Q2 != "no":
                 print("\nPlease enter either 'yes' or 'no'")
                 Q2=input("Is this tax code for the income tested benefit? ")
                 TaxCode='' #define it before using
                 if Q2=="yes":
                     TaxCode = "M"
                 elif Q2=="no":
                      print (M1)
                      exit(0) # terminates program and raises success non-zero value refers failure.
    
    print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)
    

    如果您在函数中使用上述代码并且想要停止该函数,您可以使用return 语句通过返回None 跳过打印最后一行,这样您的代码将是。

    while Q1 != "yes" and Q1 != "no":
        print("\nPlease enter either 'yes' or 'no'")
        Q1=input("Do you recieve an income tested benefit? ")
        if Q1=="yes":
            Q2=input("Is this tax code for the income tested benefit? ")
             while Q2 != "yes" and Q2 != "no":
                 print("\nPlease enter either 'yes' or 'no'")
                 Q2=input("Is this tax code for the income tested benefit? ")
                 TaxCode='' #define it before using
                 if Q2=="yes":
                     TaxCode = "M"
                 elif Q2=="no":
                      print (M1)
                      return None # skips print and returns to calling function.
    print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)
    

    您可以根据自己的逻辑使用exitreturn,因为您不清楚这段代码是在函数中使用还是在我解释过的主函数中使用。

    【讨论】:

    • 如果提供的任何解决方案可以解决您的问题,请接受它作为答案,或者如果它有帮助,请点赞。人们花时间解决您的问题。
    【解决方案2】:

    您应该在 if 语句之外定义“TaxCode”。

    例如,

    TaxCode = ""
    Q1=input("Do you receive an income tested benefit? ")
    while Q1 != "yes" and Q1 != "no":
        print("\nPlease enter either 'yes' or 'no'")
        Q1=input("Do you recieve an income tested benefit? ")
    if Q1=="yes":
                     Q2=input("Is this tax code for the income tested benefit? ")
                     while Q2 != "yes" and Q2 != "no":
                         print("\nPlease enter either 'yes' or 'no'")
                         Q2=input("Is this tax code for the income tested benefit? ")
                     if Q2=="yes":
                                    TaxCode = "M"
                     elif Q2=="no":
                                      print (M1)
    
    print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)
    

    【讨论】:

      【解决方案3】:

      就像@Navieclipse提到的,你需要在使用前定义TaxCode。

      name = input("What is your name? ")
      while True:
          try:
              income = int(input("What is your income? ($) "))
              break
          except ValueError:
              print ("Invalid input\nPlease enter a figure")
              continue
          else:
              break
      
      TaxCode = ""
      M1  = "This program cannot determine your tax code. Please use the program for secondary income "
      
      print("Please answer the questions with 'yes' and 'no'")
      
      while True:
          Q1=input("Do you receive an income tested benefit? ")
          if Q1 == "yes":
              Q2=input("Is this tax code for the income tested benefit? ")
              if Q2 == "yes":
                  TaxCode = "Test"
                  print("Thanks for answering the questions, "+ name + ", " + "your tax code is " + TaxCode + ".")
                  break
              elif Q2 == 'no':
                  print(M1)
                  break
          else:
              print("\nPlease enter either 'yes' or 'no'")
      

      此外,在此处复制后对其进行格式化会使其更易于阅读。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多