Python Help: Convert Celsius to Fahrenheit

  • Context: Python 
  • Thread starter Thread starter TromboneNerd
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around a Python programming assignment that involves converting temperatures from Celsius to Fahrenheit. Participants are addressing issues related to user input handling and conditional statements in the code, particularly focusing on how to stop the conversion process when the user inputs 0.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their issue with the code, noting that entering 0 does not terminate the loop as intended, and they seek assistance with their if statement.
  • Another participant points out that the method Celsius.rstrip() returns a string, while int(Celsius.rstrip()) converts it to an integer.
  • A third participant suggests a modification to the if statement, recommending that the comparison should check if Celsius.rstrip() equals "0" instead of 0.
  • One participant expresses a concern about posting in the wrong forum and requests a moderator to delete the thread.

Areas of Agreement / Disagreement

There is no consensus on the solution to the problem, as participants are providing different suggestions and clarifications without agreeing on a definitive fix.

Contextual Notes

The discussion highlights potential misunderstandings regarding data types in Python, specifically the handling of strings and integers, but does not resolve the underlying issue in the code.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
11
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K