2016-11-28 49 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

我们还没有学会返回函数,我们被告知我们不应该使用它。谢谢 –