Python 2.7: Variable defined in previous function, receiving undefined error -
so variable defined in inputnfo(), why getting undefined error? try & except perhaps? i've added removed... swapped around , cannot seem find solution, , answers online seem situation based... in advance :)
super new & improved edit: getting unboundlocalerror
import random alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] strgen = [] retry = 0 ### defining def inputnfo(): global = input('how long want random word/lucky number be: ') global = raw_input('would letters or numbers?(let,num, or mix?):').lower def generate(): while > 0: if == 'let': strgen.append(random.choice(alpha)) -= 1 print '.' elif == 'num': strgen.append(random.randint(1,9)) -= 1 print '.' elif == 'mix': mixer = random.choice([0,1]) if mixer == 0: strgen.append(random.choice(alpha)) -= 1 print '.' elif mixer == 1: strgen.append(random.randint(1,9)) -= 1 print '.' def finish(): finito = ''.join(strgen) print 'generation completed!\n' if == 'let': print 'your randomly generated string is:' + finito elif == 'num': print 'your randomly generated number is:' + finito elif == 'mix': print 'your randomly generated alpha-numerical string is:' + finito ### running inputnfo() while != 0: generate() finish()
its because variable "much" in function inputnfo() local function alone. why getting undefined error in while loop. there 2 solution 1. make variable "much" global including line
def inputnfo(): global try:
and removing argument of generate function
or 2. let function inputnfo() return , use return value in while loop , generate function
do same variable "which" , put line which = ""
befor
which = "" def inputnfo(): global
Comments
Post a Comment