简体   繁体   中英

how to loop from the start when i want to keep using my calculator. and after i use it i want it to close if i type 9

When I want to quit my calculator it works, but when I want to keep using it there comes a problem:

File "main.py", line 54, in <module>
    main()
NameError: name 'main' is not defined

I want to keep looping it from the start if I type y or Y so that I could keep using it.

And if I type 9 after I use it, I can't come to the closing screen. So I want it to show the close screen and stop looping.

here's my code:

print("calculator")
print("1.Pluss")
print("2.Minus")
print("3.Ganging")
print("4.Deling")
print("9.Avslutt")


chr=int(input("1-4 eller Avslutt: "))
while True:
    if chr==1:
        a=int(input("Number 1:"))
        b=int(input("Number 2:"))
        c=b+a
        print("sum = ", c)
    elif chr==2:
        a=int(input("Number 1:"))
        b=int(input("Number 2:"))
        c=a-b
        print("sum = ", c)
    
    elif chr==3:
        a=int(input("Number 1:"))
        b=int(input("Number 2:"))
        c=a*b
        print("sum = ", c)
    
    elif chr==4:
        a=int(input("Number 1:"))
        b=int(input("Number 2:"))
        c=a/b
        print("sum = ", c)

    elif chr==5:
        print("1-4 idiot")
    
    elif chr==6:
        print("1-4 idiot")
    
    elif chr==7:
        print("1-4 idiot")
    
    elif chr==8:
        print("1-4 idiot")
        

    else:
        print("Thank you for using my calculator!")
        again = input("Do you want to keep using my calculator? y/n")


        if again == "y" or again == "Y":
            
            main()
        else:
            print("Thank you!")
            break

It works if I want to quit, but it can't keep looping from the start after it comes to.

     print("Thank you for using my calculator!")
        again = input("Do you want to keep using my calculator? y/n")

        if again == "y" or again == "Y":            
            main()
        else:
            print("Thank you!")
            break

If you are offering an option to terminate (9. Avslutt) you don't need to ask user for confirmation of iteration. Just repeat the input prompts.

There's no need to convert the user input to int as the values are not used in any arithmetic context - ie, they're just strings.

You may find this pattern useful:

from operator import add, sub, mul, truediv

OMAP = {'1': add, '2': sub, '3': mul, '4': truediv}

while True:
    print("calculator")
    print("1. Pluss")
    print("2. Minus")
    print("3. Ganging")
    print("4. Deling")
    print("9. Avslutt")
    if (option := input('1-4 eller avslutt: ')) == '9':
        break
    if (op := OMAP.get(option)) is None:
        print('Ugyldig alternativ')
    else:
        x = float(input('Number 1: '))
        y = float(input('Number 2: '))
        print(f'Resultat = {op(x, y):.2f}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM