Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Basic python help?

  1. Oct 7, 2011 #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​
     
  2. jcsd
  3. Oct 7, 2011 #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
     
  4. Oct 7, 2011 #3

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    note that Celsius.rstrip() is a string
    int(Celsius.rstrip()) is an integer
     
  5. Oct 8, 2011 #4

    jhae2.718

    User Avatar
    Gold Member

    I took your code and put it in [ code ] tags.
    Code (Text):

    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 (Text):
    [COLOR="Orange"]if[/COLOR] Celsius.rstrip() == "0":
    as your test.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Basic python help?
  1. Help in Python rk4 (Replies: 1)

  2. Basic Python questions (Replies: 5)

  3. Basic python question (Replies: 2)

Loading...