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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top