我一直试图完成我的任务,但我遇到了逻辑错误。我使用的是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()
我得到了这个输出:
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。我做错了什么?
答案 0 :(得分:1)
您不应该在main()
中再次致电checker
。您可以返回(如果您将其置于循环中,也可以使用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'
中是'n'
还是main
。
编辑:
如果您不想使用return
,您可以取出循环else
,但这样您就无法检查用户是否想要停止:< / p>
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!")
答案 1 :(得分:0)
您没有使用for
循环。来自checker()
,您再次致电main()
。
而不是
else: main()
你应该只是return
。
我不确定你在checker()
做了你想做的事。