Do while loops have local variables in Python? -
i trying use while statement so:
o = 0 while o == 0: try: n = int(raw_input("which number want begin with?")) o = 1 except: o = 0 print "please use valid number."
however, when try use variable n later, gives me "local variable 'n' referenced before assignment' unboundlocalerror. means n cannot recognized variable in def using, because exists in while statement? possible? whole code:
import time sys import argv import os os.system("cls") print "welcome number counter 2.0!" = true def program(): global if == false: os.system("cls") o = 0 while o == 0: try: n = int(raw_input("which number want begin with?")) o = 1 except: o = 0 print "please use valid number." if n == "/historykeep false": if == false: print "command historykeep set false." else: = false print "command set successfully." elif n == "/historykeep true": if == true: print "command historykeep set true." else: = true print "command set successfully." if n == "/historykeep false": n = raw_input("which number want begin with?") elif n == "/historykeep true": n = raw_input("which number want begin with?") d = raw_input("how many seconds between each number?") d = int(d) total_s = n * d while n > 0: print n time.sleep(d) n = n - 1 print "done in", total_s, "seconds in total!" end_q = raw_input("exit or retry? (e/r)") if end_q == "e": os.system("cls") print "exiting." time.sleep(0.5) os.system("cls") print "exiting.." time.sleep(0.5) os.system("cls") print "exiting..." time.sleep(0.5) os.system("cls") exit(0) elif end_q == "r": program() program()
you set a = true
@ beginning. test if a == false
, set n
if is. test n == "/history...
. n
has not been set @ point.
you need make sure n assigned before use it. not enough mention in branch not taken.
Comments
Post a Comment