2016-11-28 7 views
0

私は私の割り当てを行うにしようとしてきたが、私はこの出力を得ることはPython 3は、関数内で同じ範囲の値を取得

print("Car Service Cost") 
def main(): 
    loan=int(input("What is your loan cost?:\t")) 
    maintenance=int(input("What is your maintenance cost?:\t")) 
    total= loan + maintenance 
    for rank in range(1,10000000): 
     print("Total cost of Customer #",rank, "is:\t", total) 
     checker() 
def checker(): 
    choice=input("Do you want to proceed with the next customer?(Y/N):\t") 
    if choice not in ["y","Y","n","N"]: 
     print("Invalid Choice!") 
    else: 
     main() 
main() 

とIMを用いた論理error.I'mに実行しました:

Car Service Cost 
What is your loan cost?: 45 
What is your maintenance cost?: 50 
Total cost of Customer # 1 is: 95 
Do you want to proceed with the next customer?(Y/N): y 
What is your loan cost?: 70 
What is your maintenance cost?: 12 
Total cost of Customer # 1 is: 82 
Do you want to proceed with the next customer?(Y/N): y 
What is your loan cost?: 45 
What is your maintenance cost?: 74 
Total cost of Customer # 1 is: 119 
Do you want to proceed with the next customer?(Y/N): here 

毎回自分のランクを1にしています。私は間違って何をしていますか?

+1

'main'は' checker'がループに入り、 'main'を呼び出します - あなたは全くループしていないので、ループの*最初の反復で' main'を再起動するだけです(再帰の深さが足りなくなります)遅かれ早かれ)。 –

答えて

1

checkermain()に再度電話しないでください。あなただけの(あなたがループの中でそれを置けばあなたもbreakを使用することができます)を返すことができます。

def checker(): 
    while True: 
     choice=input("Do you want to proceed with the next customer?(Y/N):\t") 
     if choice not in ["y","Y","n","N"]: 
      print("Invalid Choice!") 
     else: 
      return 

あなたが入力され'n'または'N'場合mainでループから抜け出すにしたい場合は、あなたが戻って試すことができます値:その後、

def checker(): 
    while True: 
     choice=input("Do you want to proceed with the next customer?(Y/N):\t") 
     if choice not in ["y","Y","n","N"]: 
      print("Invalid Choice!") 
     else: 
      return choice.lower() 

そして、それはmain'y'または'n'であるかどうかを確認します。

編集: あなたがreturnを使用したくない場合は、単にループを取り出し、elseが、ユーザーを停止したい場合は、この方法は、あなたがチェックすることができませんすることができます

def checker(): 
    choice=input("Do you want to proceed with the next customer?(Y/N):\t") 
    if choice not in ["y","Y","n","N"]: 
     print("Invalid Choice!") 
0

ループはforループを使用していません。 checker()から再度main()を呼び出しています。

代わりの

else: main() 

次のことを行う必要があり、単純にreturn


checker()で意図したことをしているとは確信していません。

+0

私たちはまだ復帰機能を学んでいませんし、私たちはそれを使うべきではないと言われています。ありがとうございました –

関連する問題