r/pythonhelp • • Mar 16 '21

HOMEWORK Celsius to Fahrenheit Table Problem

The problem is: Write a program that displays a table of the Celsius temperatures 0 through 20 and the Fahrenheit equivalents. Use a loop to display the table.

The instructor wants the program to ask the user to input the beginning and ending Celsius temperatures instead of using 0 to 20. Is should also be created with a nested loop with the outer loop stopping the program when 0 is inputted for beginning and end temps and the inner loop calculates and prints the result.

So far I have this:

celsius = 0.0

fahrenheit = 0.0

user_celsius_beg = 0.0

user_celsius_end = 0.0

user_celsius_beg = int(input('Enter a beginning Celsius temperature: '))

user_celsius_end = int(input('\nEnter an ending Celsius tempeperature: '))

print("\nCelsius Temperature\tFahrenheit Temperature")

print('-----------------------------------------------')

for celsius in range(user_celsius_beg, user_celsius_end + 1):

fahrenheit = ((9 / 5) * celsius) + 32

print("\t", celsius, "\t\t\t", format(fahrenheit, ".2f"))

I had an idea of using this but when I do it keeps repeating the same values over and over in the table. I know why it keeps repeating the values but I do not know how to only have it display once. So my question is, is there a way to create this with a nested loop? :

while not(user_celsius_beg == 0 and user_celsius_end == 0):

for celsius in range(user_celsius_beg, user_celsius_end + 1):

fahrenheit = ((9 / 5) * celsius) + 32

print("\t", celsius, "\t\t\t", format(fahrenheit, ".2f"))

2 Upvotes

3 comments sorted by

1

u/Tomboyish44 Mar 16 '21

Ayo, the break statement in the body of the while loop will do the trick I guess, run this:

celsius = 0.0
fahrenheit = 0.0

user_celsius_beg = int(input('Enter a beginning Celsius temperature: '))

user_celsius_end = int(input('\nEnter an ending Celsius tempeperature: '))

print("\nCelsius Temperature\tFahrenheit Temperature")

print('-----------------------------------------------')

while user_celsius_beg != 0 and user_celsius_end != 0:
    for celsius in range(user_celsius_beg, user_celsius_end + 1):
        fahrenheit = ((9 / 5) * celsius) + 32
        print("\t", celsius, "\t\t\t", format(fahrenheit, ".2f"))
    break

The output will look something like this: https://prnt.sc/10ne77j

1

u/Tomboyish44 Mar 16 '21

Also, I removed this from the code:

user_celsius_beg = 0.0
user_celsius_end = 0.0

Reason being both variables were declared again beneath it anyway without any usage before it, so yeah

1

u/structurefall Mar 16 '21

I just wanted to note- you've got a variable called celcius that you're declaring at the beginning, and then another in that for statement with the range later. Not a good practice, likely to get you in trouble later! It's especially notable here because the first is declared as a float and then you're explicitly using it as an int later.