'How to loop in python multiples times
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()
Solution 1:[1]
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()
Solution 2:[2]
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()
Solution 3:[3]
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 :)
Solution 4:[4]
This code should work
choice = 'y';
while choice != 'e':
choice = input('If you want to convert c to f press a\nIf you want to convert f to c press b\nIf you want to exit press e');
if choice == 'a':
c = int(input('Enter temp in C'));
print("F =", c*1.8+32);
elif choice == 'b':
f = int(input('Enter temp in F'));
print("C =", (F-32)/1.8);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tal Folkman |
| Solution 3 | Hassan |
| Solution 4 | Hiten Tandon |
