Python Help: Convert Celsius to Fahrenheit

  • Thread starter TromboneNerd
  • Start date
  • Tags
    Python
In summary, the conversation is about a student having trouble with a basic python assignment that asks the user for temperatures in Celsius and converts them to Fahrenheit. The problem is that the program is not stopping when the user inputs 0, and is instead converting it like a normal number. The student provided their code and realized it was in the wrong forum. They also mentioned that the input for Celsius needs to be converted to an integer. A suggestion was made to refactor the code for a more efficient solution.
  • #1
TromboneNerd
18
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
  • #2
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
 
  • #3
note that Celsius.rstrip() is a string
int(Celsius.rstrip()) is an integer
 
  • #4
I took your code and put it in [ code ] tags.
Code:
count = 1
converted = [COLOR="DarkOrchid"]False[/COLOR]
[COLOR="Orange"]while[/COLOR](converted == [COLOR="DarkOrchid"]False[/COLOR]):
    Celsius = [COLOR="DarkOrchid"]raw_input[/COLOR]([COLOR="SeaGreen"]"Temperature number "[/COLOR] + [COLOR="DarkOrchid"]str[/COLOR](count) + [COLOR="SeaGreen"]" to be converted?: "[/COLOR])
    [COLOR="Orange"]if[/COLOR] (Celsius.rstrip() == 0):
        converted = [COLOR="DarkOrchid"]True[/COLOR]
    Celsius = [COLOR="DarkOrchid"]int[/COLOR](Celsius.rstrip()) * 1.8 + 32
    [COLOR="DarkOrchid"]print[/COLOR](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:
[COLOR="Orange"]if[/COLOR] Celsius.rstrip() == "0":
as your test.
 
  • #5


First, I would suggest using the built-in function input() instead of raw_input() as it will automatically convert the user's input to a number. Additionally, you should be using the integer 0 in your if statement instead of the string "0". This is because when you use the rstrip() function, it will remove any trailing whitespace, so the input of 0 will become an empty string, which will not equal 0. So your if statement should be if (Celsius == 0). This should solve the issue and properly stop the loop when the user enters 0.
 

What is the formula for converting Celsius to Fahrenheit?

The formula for converting Celsius to Fahrenheit is: Fahrenheit = (Celsius * 9/5) + 32

How do I write a function to convert Celsius to Fahrenheit in Python?

To write a function in Python to convert Celsius to Fahrenheit, you can use the formula mentioned above and create a function with the input parameter as the Celsius value. For example: def celsius_to_fahrenheit(celsius): return (celsius * 9/5) + 32

What is the difference between Celsius and Fahrenheit?

Celsius and Fahrenheit are two different units of temperature measurement. The main difference between them is their scale. Celsius uses a scale of 0 to 100, where 0 represents the freezing point of water and 100 represents the boiling point of water. Fahrenheit uses a scale of 32 to 212, where 32 represents the freezing point of water and 212 represents the boiling point of water.

How do I convert a temperature in Fahrenheit to Celsius?

To convert a temperature in Fahrenheit to Celsius, you can use the formula: Celsius = (Fahrenheit - 32) * 5/9. Alternatively, you can create a function in Python similar to the one mentioned above, but with the Fahrenheit value as the input parameter.

Can I convert negative Celsius values to Fahrenheit?

Yes, you can convert negative Celsius values to Fahrenheit. The formula for converting Celsius to Fahrenheit will still work. For example, -10 degrees Celsius is equivalent to 14 degrees Fahrenheit.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
283
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
881
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
320
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top