Reputation: 5
I've been trying to do my assignment but I've run into an logic error.I'm using 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()
And im getting this output:
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
I'm having my rank as 1 every time. What am I doing wrong?
Upvotes: 0
Views: 51
Reputation: 859
You shouldn't call main()
again in checker
. You can just return (you can also use break
if you put it in a loop):
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
If you want to break out of the loop in main
if 'n'
or 'N'
is entered, then you can try returning a value:
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()
And then check if it is 'y'
or 'n'
in main
.
Edit:
If you don't want to use return
you can just take out the loop and else
, but this way you wouldn't be able to check if the user wanted to stop:
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!")
Upvotes: 1
Reputation: 4866
You are not using the for
loop. From checker()
you are calling again main()
.
Instead of
else: main()
you should simply return
.
I'm not sure you are doing what you intended in checker()
.
Upvotes: 0