Home > front end >  How to convert Celsius to Fahrenheit and vice-versa in python
How to convert Celsius to Fahrenheit and vice-versa in python

Time:10-19

I'm trying to create a program that will convert Celsius to Fahrenheit and vice-versa.

The first thing the program will do is ask the user what the user wants to convert either Celsius or Fahrenheit. If the input is invalid, print invalid and ask to try again.

Then will ask the user to input the start, end, and interval separated by an asterisk. For example 0102. This means that the start of the conversion will be from 0 to 10 with an interval of 2, thus the value to be converted will be 0, 3, 6, 9

If the start<end, then the interval should be 0<interval otherwise, your program will display an error and ask to try again

Or

If the start>end, then the interval should be 0>interval otherwise, your program will display an error and ask to try again.

If the user inputs has only one value, like 10. This means that the start is equal to 1 which will be the default value for start, up to 10, and the interval is 2 which will be the default value of the interval if start<end.

If the user input has two values like 10*2, this means that the start is equal to 10 up to 2. The interval is set to the default value which is equal to -2 since start>end.

This is my code, it doesn't work and I'm stuck. And how am I gonna use for loop here?

while True:
    pick = input("Enter your input either in Celsius or Fahrenheit: ")
    if pick == "Celsius":
        pass
    elif pick == "Fahrenheit":
        pass
    else:
        print("Invalid input. Try again.")
        continue
    while True:
        sei = input("Enter the start, range, and interval separated by an asterisk(*): ").split("*")
        if len(sei) == 3:
            if int(sei[0].isdigit()) < int(sei[1].isdigit()) and 0 < int(sei[2].isdigit()):
                pass
            elif int(sei[0].isdigit()) > int(sei[1].isdigit()) and 0 > int(sei[2].isdigit()):
                pass
            else:
                print("Error. Try again.")
                continue
        else:
            print("Error. Try again")

And If the input is correct, the output will be in two columns like:

Input is "0122"

Output: Celsius. Fahrenheit 0 32.0 3 37.4 6 42.8 9 48.2 12 53.6

Then the program will ask the user to try again. If yes, the program will run from the very start. If no, it'll print "Thank you" and the number of invalid inputs in the whole program and the number of times the user successfully converts temperature.

CodePudding user response:

Here is my solution (inefficient maybe, I'm a beginner too) to your problem.

temperatureValues = []
while True:
    pick = input("Enter your input either in Celsius or Fahrenheit: ")
    if pick == "Celsius":
        pass
    elif pick == "Fahrenheit":
        pass
    else:
        print("Invalid input. Try again.")
        continue
    while True:
        sei = input("Enter the start, range, and interval separated by an asterisk(*): ").split("*")
        if len(sei) == 3:
            if int(sei[0]) < int(sei[1]) and 0 < int(sei[2]):  # normal case
                for i in range(int(sei[0]), int(sei[1]), int(sei[2])):
                    temperatureValues.append(i)
                break
            elif int(sei[0]) > int(sei[1]) and 0 > int(sei[2]):  # reverse case
                for i in range(int(sei[1]), int(sei[0]), int(sei[2])):
                    temperatureValues.append(i)
                break
            else:
                print("Error. Try again.")
                continue
        elif len(sei) == 2:
            for i in range(int(sei[0]), int(sei[1]), 2):
                temperatureValues.append(i)
            break
        elif len(sei) == 1:
            for i in range(1, int(sei[0]), 2):
                temperatureValues.append(i)
            break
        else:
            print("Error. Try Again.")
            continue
    print(temperatureValues)
    #  Implement your conversion here, by performing the conversion operation on each value of the temperatureValues list.

I would also advise you to do comparison in values by writing the variable first. Like int(sei[0]) > 0, instead of writing this in reverse. Makes the code more readable. Best of luck!

  • Related