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 where the user is tasked with converting temperatures from Celsius to Fahrenheit. The main issue arises when the user inputs 0, which is intended to signal the end of input. Instead of stopping, the program incorrectly converts 0 to Fahrenheit, resulting in an output of 32. The problem lies in the if statement that checks for the termination condition. The suggested solution is to modify the condition to check if the input, after stripping whitespace, equals the string "0" rather than the integer 0. Additionally, there are recommendations to refactor the code for better readability and adherence to Python conventions.
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 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
 
note that Celsius.rstrip() is a string
int(Celsius.rstrip()) is an integer
 
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.
 
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