(Dec-11-2017, 09:33 PM)j.crater Wrote: There are a few issues. First, which version of Python interpreter are you using? (2.7, 3.5, ...). I currently don't have Python 2 installed (which this code seems to be written for), so I didn't give it a test run. But I'm surprised it would run at all.
The information you ask user (planet, lbs) get lost (out of scope) when functions ask_planet and ask_weight are finished. So your calculation cannot access mass, and planet_call cannot access planet. You need to return those values from functions and pass them as arguments to functions that use them.
I do not know what version of python it is, whichever version is built into CodeHS. Also, I do not really understand where I would use the return functions.
(Dec-11-2017, 09:35 PM)squenson Wrote: In python, when you declare a variable in a function, it is only available in this function. So planet is only available in ask_planet() and disappears as soon as the function ends, unless you declare the variable as a global one.
How do I declare it as a global variable? Would I just set each variable as a zero or something at the very beginning?
Alright, using the help offered, I have come up with these, however nothing works still, and no error will pop up.
planet = 0 lbs = 0 mass = 0 mercury = 0 venus = 0 mars = 0 jupiter = 0 saturn = 0 uranus = 0 neptune = 0 pluto = 0 #Asks planet to be calculated def ask_planet(): planet = input("Which planet would you like to know your weight for? ") return planet #Asks for weight of user def ask_weight(): lbs = float(input("How many pounds do you weigh? ")) return lbs #Asks for the weight of user if to be done again def another_weight(): another_weight = input("Would you like to change the calculated weight? ") if another_weight == "No": return ask_planet() elif another_weight == "Yes": return ask_weight() return ask_planet() else: print "That's not a verified command, please enter yes or no." return another_weight() #To be used for restarting for another planet def another_planet(): another_time = input("Would you like to know your weight on another planet? ") if another_time == "No": break elif another_time == "Yes": return another_weight() else: print "That's not a verified command, please enter yes or no." return another_planet() def calculation(): mass = lbs/9.807 mercury = mass*3.7 venus = mass*8.87 mars = mass*3.71 jupiter = mass*24.92 saturn = mass*10.44 uranus = mass*8.87 neptune = mass*11.15 pluto = mass*0.58 return mercury return venus return mars return jupiter return saturn return uranus return neptune return pluto #Calls the planet that the user wants to know their weight on def planet_call(): if planet == "Mercury": print "Your weight on Mercury would be " + str(mercury) + " pounds." elif planet == "Venus": print "Your weight on Venus would be " + str(venus) + " pounds." elif planet == "Mars": print "Your weight on Mars would be " + str(mars) + " pounds." elif planet == "Jupiter": print "Your weight on Jupiter would be " + str(jupiter) + " pounds." elif planet == "Saturn": print "Your weight on Saturn would be " + str(saturn) + " pounds." elif planet == "Uranus": print "Your weight on Uranus would be " + str(uranus) + " pounds." elif planet == "Neptune": print "Your weight on Neptune would be " + str(neptune) + " pounds." elif planet == "Pluto": print "Your weight on Pluto would be " + str(pluto) + " pounds." else: print "That is not a verified planet." print "This program will calculate how much you weigh on another planet." print "The weight asked will be in pounds." print "Available planets are: Mercury, Venus, Mars, Jupiter, Saturn, Uranus, Neptune, and Pluto" ask_weight() ask_planet() calculation() planet_call() another_planet()I added the variables at the top to create the variables outside of the call functions, and I added returns to set the variables with their new value, however I still cannot find out what is wrong. I tried without setting the variables to 0 at the start, and that did not work either.
As for the version of Python I am running, I searched all over the place, and could not figure out what version CodeHS is running.