Python Question

Getting the same range value in a function. Help please

I've been trying to do my assingment but i've run into an logic error.
I would be pleased if you could help me with it. 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 everytime. What am i doing wrong?

Answer

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!")
Comments