2016-11-28 54 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() 

並即時得到這個輸出邏輯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' - >你根本沒有循環,只需在循環的每次* first *迭代中重新啓動'main'(並且你將用完遞歸深度遲早)。 –

回答

1

您不應該在checker中再次致電main()。你可以返回(你也可以使用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 

如果你想在main打出來的迴路,如果'n''N'輸入的,那麼你可以嘗試返回值:

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

然後檢查它是否'y'main'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

我們還沒有學會返回函數,我們被告知我們不應該使用它。謝謝 –