0

I am fairly new to Python, and I am currently using Python 3.3.3 just in case you were wondering. Below is my code, which I translated from whatever language Batch Script is written with, to Python. Here is the Batch Script code. I don't know what I am doing wrong, but when I run this, I get NameError: global name 'change' is not defined. I used http://pych.atomidata.com/code to check the syntax, and if you input my Python code into the box to check it, all you get is a couple code errors, and some Pep-8 errors. I can't think of what else is wrong. I don't even know what most of the code errors are, and the Pep-8 errors are all Line: __ Column: __ E302 expected 2 blank lines, found 1.

import subprocess as sp

global change
global count

def numberInput():
    while True:
        try:
            number = int(input("Input a number, please: "))
            sp.call('cls', shell=True)
            break
        except ValueError:
            print('Please enter a valid input...')
            sp.call('pause', shell=True)
            numberInput()

    change = number
    numberVerify()

def numberVerify():
    if change == 0:
        numberCorrectIsTrue()
    else:
        number = change

def numberCorrectIsTrue():
    count = count + 1
    integer = number

    # checks to see if the input
    # is valid or not.
    if number != integer:
        sp.call('cls', shell=True)
        print("Please enter a valid number!")
        loop()
    else:
        even = number % 2

    if even == 0:
        print("Substituting x in 'x / 2' with {}".format(number))
        answer = number / 2
    else:
        print("Substitiuting x in '(3 * x) + 1' with {}".format(number))
        answer = number * 3
        answer = answerOdd + 1

    print(answer)

    if answer == 1:
        returnStats()
    else:
       numberCorrectIsTrue()

def returnStats():
    print("Your original input of {} returned as 1.".format(change))
    print("A total of {} operations were executed.".format(count))
    change = change + 1
    count = 0
    sp.call('pause', shell=True)
    numberVerify()

def check():
    number = answer
    numberCorrectIsTrue()

numberInput()
James Lynch
  • 181
  • 1
  • 2
  • 11

3 Answers3

2

These lines:

global change
global count

should just be

change = 0
count = 0

The global keyword is used when you want to refer to a global variable, so a function using these values should be as such:

def func():
    global count
    count = count + 1
William
  • 2,695
  • 1
  • 21
  • 33
  • thats what I had originally, but that was throwing errors also. I was just getting `UnboundLocalError: local variable 'count' referenced before assignment` – James Lynch Aug 01 '14 at 00:51
  • @JamesLynch It was assuming that `count` was a local variable, not yet assigned. It's enough to communicate that `count` is actually global, see updated answer :) – William Aug 01 '14 at 00:56
  • ok, I changed my code to fit your answer, so that fixed the `UnboundLocalError`, but I am now getting `Line: __ Column: __ local variable '____' is assigned to but never used` and `Line: __ Column: __ local variable '____' (defined in enclosing scope on line 4) referenced before assignment` – James Lynch Aug 01 '14 at 00:59
  • 1
    what you have answered does not even cover 25 percent of what is wrong in the code – Padraic Cunningham Aug 01 '14 at 01:02
1

global in a code block isn't inherited by functions inside that code block. Every function that needs to assign to a global variable needs to have its own global declaration for that variable.

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

Add "global change" at the beginning of each function that assigns a value to change or count.

So for instance, numberInput() should look like

def numberInput():
    global change
    while True:
    ...

Source: Using global variables in a function other than the one that created them

Community
  • 1
  • 1
bsuire
  • 1,383
  • 2
  • 18
  • 27
  • OK, that's improved my code, so now I get no errors when running the code through the shell, but now, whenever I enter my input for `int(input("Input a number, please:"))`, it just acts like nothing happened, and shows `>>>` on the next line – James Lynch Aug 01 '14 at 01:04
  • Well at least no syntax error! Unfortunately I won't be able to help any further without knowing about your new problem. I suggest you try troubleshooting a little, then make a new post more specific. – bsuire Aug 01 '14 at 01:23
  • I noticed in your code though that as long as your input is a number other than 0, your program terminates at numberVerify(), without encountering any print statements. That's probably why you don't see any output in the shell. – bsuire Aug 01 '14 at 01:29