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
#1

Alright so here's the issue, I cannot figure out why this will not work. I have tried everything. No error pops up when I run it. I even tried placing a print statement on the first line just to see if that would pop up, and it did not. The console was just blank.
The program is supposed to find out an inputted weight and determine how much you'd weigh on another planet.

#Asks planet to be calculated
def ask_planet():
    planet = input("Which planet would you like to know your weight for? ")

#Asks for weight of user
def ask_weight():
    lbs = float(input("How many pounds do you weigh? "))

#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":
        ask_planet()
    elif another_weight == "Yes":
        ask_weight()
        ask_planet()
    else:
        print "That's not a verified command, please enter yes or no."
        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":
        another_weight()
    else:
        print "That's not a verified command, please enter yes or no."
        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

#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()
Reply
#2
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.
break will not do what you expect in the if statement, it is used in for and while loops. You can restructure your if statements and check whether user says "yes", "whatever else", or he says "no", in which case don't do anything (else). And print says "please enter yes or no". Your ifs are case sensitive, so only "Yes" and "No" will be accepted. You can get around this with using string's lower() method.
Reply
#3
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.
Reply
#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
#5
http://lmgtfy.com/?q=python+declare+a+va...+as+global
Reply
#6
The calculation function doesn't return anything.
You can add the planet as a parameter and create a dict.

lbs = ask_weight()
def calculation(lbs, planet):
    mass = lbs/9.807
    weight = {
        '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} # pluto. Actually Pluto is no more a part of the nine. http://www.bbc.com/news/science-environment-33462184

     return weight[planet]

def ask_weight():
    lbs = float(input("How many pounds do you weigh? "))
    return lbs # the function have to return a value. lbs is a local to the fuction variable.

def ask_planet():
    planet = input("Which planet would you like to know your weight for? ")
    return planet # same here
And then:
 
    planet = ask_planet().lower()
    lbs = ask_weight()

    # if is not needed because we have a dict and we using the planet name to determine the desired value
    print("Your weight on {} would be {} pounds.".format(planet.title(), calculation(lbs, planet))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
You should avoid globals whenever possible. In your case, they are not needed. You are just not using functions correctly. For instance, in order for 'planet_call' to work, you need two pieces of information: the planet and the calculation for that planet (which we will call 'pounds' for now). Now we know our function is
def planet_call(planet, pounds):
You get the value of 'planet' by calling function 'ask_planet()', you get the value from the input(), so you just need to 'return' that value.
def ask_planet():
    planet = input("Which planet would you like to know your weight for? ")
    return planet
The same holds true for the function 'calculation()', in which you need to know the value of 'planet', so:
def calculation(planet):
    # Do calculation. Note you need your 'if/elif/else' here
    return pound
To use them, you need to call them:
user_planet = get_planet()
user_pounds = calculation(user_planet)
user_final = planet_call(user_planet, user_pounds)
Your 'planet_call()' only needs one print statement, the pythonic way would be:
print("Your weight on {} would be {} pounds".format(planet, pounds))
For now, forget about asking for new planets. See if you can get the 'base' working correctly. As was also pointed out, Python is case sensitive so make sure your variables are of the same case.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
(Dec-11-2017, 11:41 PM)sparkz_alot Wrote: Your 'planet_call()' only needs one print statement, the pythonic way would be:
print("Your weight on {} would be {} pounds".format(planet, pounds))
For now, forget about asking for new planets. See if you can get the 'base' working correctly. As was also pointed out, Python is case sensitive so make sure your variables are of the same case.

I do not think the format command is in this version of python.

Edit: I figured out in this version of python it would be:
print("Your weight on %s would be %d pounds."%(planet, pounds))
Reply
#9
(Dec-12-2017, 12:00 AM)Korbrent Wrote: I do not think the format command is in this version of python.
Since this is a homework I presume that at school you are using Python 2. This version will be maintained by 2020. Almost no one is coding in Python 2 anymore. It is used in systems where it's already installed and lots of scripts are running on it and rewriting them will be an enormous effort.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Alright, so on the subject of variables, I did some messing around to try to do what sparkz_alot said, and I took a couple lines out and came up with this little "test" to see if it would work...

def ask_weight():
    lbs = float(input("How many pounds do you weigh? "))
    return lbs
ask_weight()
print str(lbs)
Even though I returned the value for lbs, it would not print the inputted value.

(Dec-12-2017, 12:00 AM)Korbrent Wrote:
(Dec-11-2017, 11:41 PM)sparkz_alot Wrote: Your 'planet_call()' only needs one print statement, the pythonic way would be:
print("Your weight on {} would be {} pounds".format(planet, pounds))
For now, forget about asking for new planets. See if you can get the 'base' working correctly. As was also pointed out, Python is case sensitive so make sure your variables are of the same case.

I do not think the format command is in this version of python.

Edit: I figured out in this version of python it would be:
print("Your weight on %s would be %d pounds."%(planet, pounds))

Nevermind, both ways work. I just forgot to add the period before format.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im so stressed because I cant figure out what I'm doing wrong. BursteXO 2 2,988 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