Python Help: Convert Celsius to Fahrenheit

  • Context: Python 
  • Thread starter Thread starter TromboneNerd
  • Start date Start date
  • Tags Tags
    Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
TromboneNerd
Messages
18
Reaction score
1
Im having trouble with a basic python assignment in one of my classes. Its supposed to ask the user for temperatures in Celsius and convert them to Fahrenheit, which it does fine. The problem is that its also supposed stop asking the user if he gives it 0, which means the end of the list of numbers. My program converts fine, but upon entering 0 it just gives me 32, which means something is wrong with my if statement and its just converting 0 like a normal number. Any help? here is my code.

count = 1
converted = False
while (converted == False):
Celsius = raw_input("Temperature number " + str(count) + " to be converted?: ")
if (Celsius.rstrip() == 0):
converted = True​
Celsius = int(Celsius.rstrip()) * 1.8 + 32
print (Celsius)
count = count + 1​
 
Physics news on Phys.org
I also just realized this is in the wrong forum. If a mod would kindly delete it ill just re-post in the correct forum. #apologies
 
I took your code and put it in [ code ] tags.
Code:
count = 1
converted = False
while(converted == False):
    Celsius = raw_input("Temperature number " + str(count) + " to be converted?: ")
    if (Celsius.rstrip() == 0):
        converted = True
    Celsius = int(Celsius.rstrip()) * 1.8 + 32
    print(Celsius)
    count = count + 1
You may want to refactor a bit, as there are some things you can do to make the code more "Pythonic". For your original question, try
Code:
if Celsius.rstrip() == "0":
as your test.