Hey everyone, I need help to figure out how to input while loop in here.
I have this program where it converts from Fahrenheit to Celsius or Celsius to Fahrenheit.
The purpose is to convert this program so that it repeats the calculation as long as the user wants.
After the user input and the calculation, I have to make the program ask the user if they want to repeat.
If the response is yes, the program will take the input again and perform the calculation. Keep doing it
as long as it's yes. Mainly I have to input a while loop for this to convert the program.
I am not sure how to start and where to put the while loop... I might have the idea but I'm not sure
how to translate it.
Any ideas?

import math

op = int(input("Please enter 1: for converting Fahrenheit -> Celsius ,  2: for converting Celsius -> Fahrenheit"))
num = int(input("Please enter the temperature"))


if op == 1:
    convert = 5/9 * (num - 32)
    print("The temperature in Celsius is" , convert)
elif op == 2:
    convert = 9/5 *num + 32
    print ("The temperature in Fahrenheit is" , convert)
else:
 print("Invalid operation : enter 1 or 2")

Recommended Answers

All 4 Replies

Try with this:

#!/usr/bin/env python-u
import math

option = 'y'
while (option == 'y'):
    op = int(input("Please enter 1: for converting Fahrenheit -> Celsius ,  2: for converting Celsius -> Fahrenheit:: "))
    num = int(input("Please enter the temperature:: "))
    if op == 1:
        convert = 5.0 / 9.0 * (num - 32)
        print("The temperature in Celsius is" , convert)
    elif op == 2:
        convert = (9.0 / 5.0 * num) + 32
        print ("The temperature in Fahrenheit is" , convert)
    else:
        print("Invalid operation")
    option = raw_input("Do you wish to try another conversion? (Enter 'y' to continue or any other key to quit):: ")

I transformed 5 to 5.0 and 9 to 9.0 to register them as floats and not integers. Otherwise the result was incorrect

Thanks, and instead of using raw_input, is there a different way I can write the input? I have not covered raw_input yet.

If you have Python3 instead of Python2 you use input, it is same as raw_input in Python2.

Oh, that makes sense now, thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.