Python novice while question

Asked 2 years ago, Updated 2 years ago, 14 views

first = input('Enter the temperature to convert:')

if first[-1] == 'C':
    while first != '.':
        c1 = first[0:-1]
        f1 = float(c1) * 9/5 + 32 
        print('The converted temperature is %.1fF. '%f1)
        first = input ('Enter the temperature to convert:')

if first[-1] == 'F':
    while first != '.':
        f2 = first[0:-1]
        c2 = float(f2 - 32) * 5/9
        print('The converted temperature is %.1fC. '%c2)
        first = input ('Enter the temperature to convert:')
first = input('Enter the temperature to convert:')

while first != '.':
    if first[-1] == 'C':
        c1 = first[0:-1]
        f1 = float(c1) * 9/5 + 32 
        print('The converted temperature is %.1fF. '%f1)
        first = input ('Enter the temperature to convert:')

    if first[-1] == 'F':
         c1 = first[0:-1]
        c2 = float(f2 - 32) * 5/9
        print('The converted temperature is %.1fF. '%f1)
        first = input ('Enter the temperature to convert:')

It is a program that converts seob and fahrenheit to each other until the input value is '.' If you do it like the 2nd chord, it works well First of all Like the first code, it's okay to convert Fahrenheit to Celsius, but after that, if you enter Celsius again, for example, 36C, you get 96.8F, and then 36F again, you get 96.8F. When I repeat after the while statement, do I only go back to the line I wrote while?

python

2022-09-22 18:50

1 Answers

Once C is entered, only the upper While statement is repeated, whether it is F or C, until C is entered again. The lower while syntax has the same problem....


2022-09-22 18:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.