Thread: Which planet?

    #1
  1. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    423

    Question Which planet?


    Ok, i have a script m working on that is supposed to calculate your weight based on the input you put in. problem is, i get this error:
    Code:
    Traceback (most recent call last):
      File "astronomy_hw.py", line 22, in -toplevel-
        selected_planet = input("Which planet do you want to be weighed on:  ")
      File "<string>", line 0, in -toplevel-
    NameError: name 'mars' is not defined
    after putting in: 200; mars
    here is the program code:
    Python Code:
    #imports string
    import string
    #lists planets
    planet_list = ["mercury","venus","mars","jupiter","saturn","uranus","neptune","pluto"]
    planet_length = len(planet_list)
    #planets gravity compared to earth
    weight_list = [0.37,0.88,0.38,2.64,1.15,1.14,1.17,0.04]
    #use to generate code later
    planet_weight_counter = 0
     
    #state ment for main while loop
    complete = False
    #prints all the planets to choose from at program launch
    while planet_weight_counter < planet_length:
        print planet_list[planet_weight_counter]
        planet_weight_counter += 1
     
    #main while loop & program starts here:        
    while complete == False:
    #gets requiredinput from user
        earth_weight = raw_input("Please Enter Your Weight on Earth:  ")
        selected_planet = input("Which planet do you want to be weighed on:  ")
        selected_planet = selected_planet.lower()
     
    #gets actual weight on another planet   
        if selected_planet in planet_list:
    #sets the planet_weight_counter variable to the appropiate number for planet & its gravity
            while not planet_list[planet_weight_counter] == selected_planet:
                planet_weight_counter += 1
    #calculates weight on other planet
            new_weight = weight_list[planet_weight_counter] * earth_weight
            print "Your weight on planet ", selected_planet, " is ", new_weight, "."
    #makes it so the loop ends after this
            complete = True
            continue
    #restarts to beginning of the loop
        else:
            print "Error: Not a Planet; Please Enter Again:  "

    thanks in advance.
  2. #2
  3. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    620
    Code:
    # * * * * * * * * * * * * * * * * * * * * * *	
    # * * * use input instead of raw_input  * * *
    # * * * * * * * * * * * * * * * * * * * * * *
    	earth_weight = input("Please Enter Your Weight on Earth:  ")    
    # * * * * * * * * * * * * * * * * * * * * * *	
    # * * * use raw_input instead of input  * * *
    # * * * * * * * * * * * * * * * * * * * * * *
    	selected_planet = raw_input("Which planet do you want to be weighed on:  ")    
    	selected_planet = selected_planet.lower() 
    	
    	#gets actual weight on another planet       
    	if selected_planet in planet_list:
    	
    		#sets the planet_weight_counter variable to the appropiate number for planet & its gravity        
    # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *	
    # * * * initialize the index - you used it to print the list of planets * * *
    # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *	
    		planet_weight_counter = 0
    This section of code needs to be changed as identified in the comments
  4. #3
  5. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    423
    tyvm , i thought raw_input was for floating numbers & input was for text. i know it matters because my code wasnt working.
    and i cant believe i missed that, i forgot to set it back to 0.
    ty
  6. #4
  7. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    620
    raw_input just accepts whatever is entered and stores it in the variable.
    input uses eval() to parse whatever was entered and (in the case of text) convert it to a variable.

    Here's a link to a good description of the difference: http://en.wikibooks.org/wiki/Programming:Python_Input_and_output

IMN logo majestic logo threadwatch logo seochat tools logo