variable assignment error

This is the place for queries that don't fit in any of the other categories.

variable assignment error

Postby mckryall » Wed Nov 27, 2013 3:51 am

Mekire: Do not delete your posts. The post has been recreated to the best of my ability.

I'm getting this error and I don't understand why:
Code: Select all
>>>
Traceback (most recent call last):
  File "<module1>", line 24, in <module>
  File "<module1>", line 5, in bottles
UnboundLocalError: local variable 'beer' referenced before assignment

Here is my code that I previously deleted for no reason:
Code: Select all
import time
beer = int(raw_input("How many bottles?\n> "))
#-----
def bottles():
   print beer, " bottles of beer on the wall, ", beer, " bottles of beer! Take one down, pass it around. "
   beer -= 1
   print beer, " bottles of beer on the wall!\n"
   time.sleep(1)
#- - -
def bottle():
   print beer, " bottle of beer on the wall, ", beer, " bottle of beer! Take it down, pass it around. ",
   beer -= 1
   print beer, " bottles of beer on the wall!\n"
   time.sleep(1)
def em_nuht_draunk():
   print beer, " bottles of beer on the wall, ", beer, " bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.\n"
   time.sleep(1)
   print "Sorry, guys."
   time.sleep(4)
   exit()
#-----
while beer >= 0:
   if beer > 1:
      bottles()
   elif beer == 1:
      bottle()
   elif beer == 0:
      em_nuht_draunk()
Last edited by Mekire on Sat Nov 30, 2013 7:47 am, edited 4 times in total.
Reason: >_<
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: variable assignment error

Postby Mekire » Wed Nov 27, 2013 4:22 am

What you should do, is actually pass values to, and return values from, functions.

Currently you are trying to modify a global variable. This is possible, but it is bad; so for the moment I am going to hold off on showing you how.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: variable assignment error

Postby mckryall » Wed Nov 27, 2013 4:41 am

How is it bad? I want it to be global so I don't have to contain it.
Last edited by Mekire on Sat Nov 30, 2013 7:26 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: variable assignment error

Postby Mekire » Wed Nov 27, 2013 8:00 am

mckryall wrote:How is it bad? I want it to be global so I don't have to contain it.

And that is exactly why I didn't show you how to do it. Global variables are bad; functions blindly changing global data make glitches impossible to find and code impossible to read. Scopes work this way for a reason; not to inconvenience you.

Pass the variable you need to the function; change it in the function; then return it from the function.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: variable assignment error

Postby mckryall » Wed Nov 27, 2013 5:29 pm

Figured out through IRC that I could just do bottles(beer) and make it return beer - 1.
Last edited by Mekire on Sat Nov 30, 2013 7:26 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: variable assignment error

Postby Kebap » Sat Nov 30, 2013 7:02 am

mckryall wrote:abcdefghijklmnopqrstuvwxyz

Why did you delete your original post? This seems somehow very impolite.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 377
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: variable assignment error

Postby micseydel » Sat Nov 30, 2013 7:57 am

It's extremely impolite. I'm going to lock every post he makes now. It's the reason we lock everyone's first post.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], tomv and 3 guests