Python Python Help: Convert Celsius to Fahrenheit

  • Thread starter Thread starter TromboneNerd
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion centers on a Python assignment involving temperature conversion from Celsius to Fahrenheit. The user successfully converts temperatures but encounters an issue where entering 0 does not terminate the input loop as intended. Instead, the program continues to convert 0 to 32, indicating a problem with the if statement. The specific line causing confusion is "if (Celsius.rstrip() == 0):", which is meant to check if the input is 0 but fails because it compares a string (the result of rstrip) to an integer (0). To resolve this, the input should be checked against the string "0" or converted to an integer before the comparison. The discussion highlights the importance of data type handling in Python and the need for proper input validation in user prompts.
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​
 
Technology news on Phys.org
I use Python. Can you explain what this portion of code does?

if (Celsius.rstrip() == 0):
converted = True
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top