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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top