Python Help: Convert Celsius to Fahrenheit

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

The forum discussion centers on a Python assignment that requires converting Celsius to Fahrenheit while terminating the input process when the user enters 0. The user’s code incorrectly processes the input of 0, converting it to Fahrenheit instead of stopping the loop. The issue lies in the condition of the if statement, where the comparison is made against an integer instead of a string. The correct approach involves checking if the input is equal to the string '0' before converting it.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with data types in Python (strings and integers)
  • Knowledge of control flow statements in Python (if statements, loops)
  • Experience with user input handling in Python
NEXT STEPS
  • Review Python data types and type conversion
  • Learn about Python control flow and loops
  • Explore error handling in Python to manage user input
  • Practice writing functions in Python for modular code
USEFUL FOR

Students learning Python programming, educators teaching basic coding concepts, and anyone looking to improve their skills in handling user input and control flow in Python.

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 use Python. Can you explain what this portion of code does?

if (Celsius.rstrip() == 0):
converted = True
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
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
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K