Home > database >  How to loop in python multiples times
How to loop in python multiples times

Time:02-20

i have assignment and we need to convert celsius to fahrenheit and vice versa. i have to loop the program but i dont know how.

my code is working but its still wrong.

def main():

print("a. Celsius to Fahrenheit")
print("b. Fahrenheit to Celsius")

choice = str(input("Enter Choice:"))

if choice == "a":
    def c_to_f(C):
        return((9/5)*C)   32
    temp =float(input("Enter Temp: "))
    print(temp,"Celsius is",c_to_f(temp),"Fahrenheit")
    
if choice =="b":
    def f_to_c(F):
        return (5/9)*(F-32)
    temp =float(input("Enter Temp: "))
    print(temp,"Fahrenheit is",f_to_c(temp),"Celsius")

repeat = input("Do you want to convert again? (Yes/No): ")

for _ in range(10):
    if repeat == "yes":
        main()
    else:
        exit()

main()

CodePudding user response:

Hi that's because variable has scope in which you can read their modifications, repeat modification in main() are not visible outside of their scope: here the main function.

Variables thatcan be "inspect" everywhere are called globals, and you should declare them at the beggining of a file outside of a function.

You have many solution to your problem, if you want to stay close from your actual code, here is one:

from sys import exit

def main():
    print("a. Celsius to Fahrenheit")
    print("b. Fahrenheit to Celsius")

    choice = str(input("Enter Choice:"))

    if choice == "a":
        def c_to_f(C):
            return((9/5)*C)   32
        temp =float(input("Enter Temp: "))
        print(temp,"Celsius is",c_to_f(temp),"Fahrenheit")
        
    if choice =="b":
        def f_to_c(F):
            return (5/9)*(F-32)
        temp =float(input("Enter Temp: "))
        print(temp,"Fahrenheit is",f_to_c(temp),"Celsius")

    return input("Do you want to convert again? (Yes/No): ")


for _ in range(10):
    repeat = main()
    if repeat.lower() == 'no':
        exit()

CodePudding user response:

The problem with your code is that you ran main() after finishing in your last line of code

This will work great for you:

def main():
    for _ in range(10):
        print("a. Celsius to Fahrenheit")
        print("b. Fahrenheit to Celsius")

        choice = str(input("Enter Choice:"))

        if choice == "a":
            def c_to_f(C):
                return ((9 / 5) * C)   32

            temp = float(input("Enter Temp: "))
            print(temp, "Celsius is", c_to_f(temp), "Fahrenheit")

        if choice == "b":
            def f_to_c(F):
                return (5 / 9) * (F - 32)

            temp = float(input("Enter Temp: "))
            print(temp, "Fahrenheit is", f_to_c(temp), "Celsius")

        repeat = input("Do you want to convert again? (Yes/No): ")
        if repeat == "yes":
            main()
        else:
            exit()


if __name__ == '__main__':
    main()

CodePudding user response:

The problem with your code is your last user input.

This will work great for you:

def main():

    print("a. Celsius to Fahrenheit")
    print("b. Fahrenheit to Celsius")

    choice = str(input("Enter Choice:"))

    if choice == "a":
        def c_to_f(C):
            return((9/5)*C)   32
        temp =float(input("Enter Temp: "))
        print(temp,"Celsius is",c_to_f(temp),"Fahrenheit")
        
    if choice =="b":
        def f_to_c(F):
            return (5/9)*(F-32)
        temp =float(input("Enter Temp: "))
        print(temp,"Fahrenheit is",f_to_c(temp),"Celsius")

    repeat = str(input("Do you want to convert again? (Yes/No): "))

    for _ in range(10):
        if repeat == "yes" or repeat == "Yes":
            main()
        else:
            exit()
if __name__ == '__main__':
    main()

Have Fun with CODE :)

  • Related