I am new to python and cant figure out what I am doing wrong with the global variables. Here is an example of the problem I am having.

number = 0

def test():
global number
number = raw_input("Please type a number.")

def prints():
global number
if number2 != 1:
print "Yes"
else:
print "NO!"

test()
prints()

In this example it prints YES no matter what number I enter. Any suggestions?

I am new to python and cant figure out what I am doing wrong with the global variables

Dont use global statement,because it is ugly.
Function should receive arguments and return value/s out.
Code should stay local to function,with global statement you are destoring that.
Use code tag,now i think it work as you expected.

#number = 0 why

def test():
    #global number ugly
    number = raw_input("Please type a number.") #return a string
    return number

def prints(number):
    #global number ugly
    #if number2 != 1: #Where is number2 coming from?
    if int(number) != 1: #make number an integer
        print "Yes"
    else:
        print "NO!"

number = test()
prints(number) #we give function test() return value as an argument to function prints()

Edited 4 Years Ago by snippsat: n/a

Thank You so much. number2 was originally a variable when I was trying to get it to work correctly.

This question has already been answered. Start a new discussion instead.