2017-04-20 12 views
1

ユーザが印刷するために「いいえ」と入力した場合(M1)、最後の印刷文が実行されないようにするにはどうすればよいですか?私はM1が印刷されるときにプログラムを終了させたい。私はエラーを受け取ります "NameError:name 'TaxCode'がブロック内に宣言されているため、ユーザーが 'no'を入力したときに '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) 

答えて

0

if文の外側に 'TaxCode'を定義する必要があります。例えば

、if文

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) 
1

NameErrorを防止するために、前に空の値でTaxCodeを定義します。そのようなTaxCode =''

ここでプログラムを終了するにはexit(0)を使用できます。inside()は成功/失敗を示します。 linuxの終了ステータスと同じです。あなたが関数内で上記のコードを使用して、あなたがそのような場合には、コードがなりますNoneを返すことで、最後の行の印刷をスキップするreturnステートメントを使用することができる機能を停止する場合はだからあなたのコードは

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) 

になります。

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) 

あなたが明確ではないので、あなたは、このコードは、私は両方の説明関数またはメインで使用されているかどうか、あなたのロジックに従ってexitまたはreturnのいずれかを使用することができます。

+0

提供されている解決策のいずれかが問題を解決する場合は、回答として受け入れるか、助けてください。人々はあなたの問題を解決するのに時間を費やします。 – Mani

0

@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'") 

また、ここにコピーした後に書式を設定すると、読みやすくなります。

関連する問題