[Tutor] renaming input works intermittently

Jim Mooney cybervigilante at gmail.com
Fri Jul 12 02:44:32 CEST 2013


When I tried a simple rename of input, it worked - in python 2.7 and python 3.3

import sys
if int(sys.version[0]) < 3:
    input = raw_input

x = input('type something ')
print(x)  # this works in both Py versions

But when I tried that in my numbers program, I got an error:
UnboundLocalError: local variable 'input' referenced before assignment
for the below:

        try:
            if int(sys.version[0]) < 3:
                input = raw_input
            numbers_str = original = input('Enter a positive'
'integer, space separated if desired.') # error occurs here
				
Oddly, the error only occurs in Python 3.3 - the above works in Python 2.7

Here it is, where I run it from the dos box. Fails in Py3 on the first
run, works in Py2 on the second run

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

Python 3.3 run doesn't work

C:\Python33\Jimprogs>py3 numrev1.py
Traceback (most recent call last):
  File "numrev1.py", line 115, in <module>
    main()
  File "numrev1.py", line 111, in main
    name_of_numbers = numbers_to_name()
  File "numrev1.py", line 75, in numbers_to_name
    triplets, triplen = create_triplets()
  File "numrev1.py", line 64, in create_triplets
    numbers_str = check_input()
  File "numrev1.py", line 40, in check_input
    numbers_str = original = input('Enter a positive'
UnboundLocalError: local variable 'input' referenced before assignment

Python 2.7 run works

C:\Python33\Jimprogs>py2 numrev1.py
Enter a positiveinteger, space separated if desired.123456
one hundred twenty-three thousand four hundred fifty-six


-- 
Jim

Although the darling of health faddists, there is no such thing as
canola oil, since there is no canola plant - it's genetically modified
rapeseed oil given a nicer name ;')


More information about the Tutor mailing list