Python Forum
Cannot figure out what is wrong
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot figure out what is wrong
#4
(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.
Reply


Messages In This Thread
Cannot figure out what is wrong - by Korbrent - Dec-11-2017, 09:13 PM
RE: Cannot figure out what is wrong - by j.crater - Dec-11-2017, 09:33 PM
RE: Cannot figure out what is wrong - by Korbrent - Dec-11-2017, 09:39 PM
RE: Cannot figure out what is wrong - by squenson - Dec-11-2017, 09:35 PM
RE: Cannot figure out what is wrong - by squenson - Dec-11-2017, 10:14 PM
RE: Cannot figure out what is wrong - by wavic - Dec-11-2017, 11:12 PM
RE: Cannot figure out what is wrong - by Korbrent - Dec-12-2017, 12:00 AM
RE: Cannot figure out what is wrong - by wavic - Dec-12-2017, 12:12 AM
RE: Cannot figure out what is wrong - by Korbrent - Dec-12-2017, 12:17 AM
RE: Cannot figure out what is wrong - by Korbrent - Dec-12-2017, 01:31 AM
RE: Cannot figure out what is wrong - by wavic - Dec-12-2017, 03:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Im so stressed because I cant figure out what I'm doing wrong. BursteXO 2 3,046 Jan-29-2018, 01:16 AM
Last Post: BursteXO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020