未绑定局部变量错误:在分配之前引用的局部变量't' | 已定义

2024-05-14 22:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个错误: UnboundLocalError: local variable 't' referenced before assignment 当我执行方程t = t + tc 在函数中。正如我之前在程序中定义的那样,这似乎没有多大意义:

#t(time) Axies
global t
t = 0

以下是整个程序:

import timelines
#x(forward-back) Axies
global x
x = 0
#y(left-right) Axies
global y
y = 0
#z(up) Axies
global z
z = 0
#t(time) Axies
global t
t = 0
#u(universe) Axies
global u
u = 1
#how much time moves by per-turn, string
global time_change
time_change = 'doggos'
#the timeline, nets of exctinction
timelineList = 'Universal Development\nSolar Development\nPlanetary Development\nEvolution\nIntelligence\nContinental Drift\nSpeech\nReligion/Beliefs\nAgriculture\nTechnological Innovation\nSocial Politics\nCommunities\nCivilizations\nWar/Conflicts\nEconomy\nLaw\n----------------------------------------------'
global loop
global turn_action
global loop
def checkAction():
    ca_loop = 1
    while( ca_loop == 1 ):
        if turn_action == 'timeline':
            which_timeline = input("What timeline would you like to view?\n----------------------------------------------\n" + timelineList + "\n[Timelines]~| ")
            if which_timeline == "Universal Development":
                print(timelines.TimeUD)
                ca_loop = 0
            elif which_timeline == "Solar Development":
                print(timelines.TimeSD)
                ca_loop = 0
            elif which_timeline == "Planetary Development":
                print(timelines.TimePD)
                ca_loop = 0
            elif which_timeline == "Evolution":
                print(timelines.TimeEv)
                ca_loop = 0
            elif which_timeline == "Intelligence":
                print(timelines.TimeIn)
                ca_loop = 0
            elif which_timeline == "Continental Drift":
                print(timelines.TimeCD)
                ca_loop = 0
            elif which_timeline == "Speech":
                print(timelines.TimeSp)
                ca_loop = 0
            elif which_timeline == "Religion" or "Beliefs" or "Religion/Beliefs":
                print(timelines.TimeRB)
                ca_loop = 0
            elif which_timeline == "Agriculture":
                print(timelines.TimeAg)
                ca_loop = 0
            elif which_timeline == "Technological Innovation":
                print(timelines.TimeTI)
                ca_loop = 0
            elif which_timeline == "Social Politics":
                print(timelines.TimeSP)
                ca_loop = 0
            elif which_timeline == "Communities":
                print(timelines.TimeCo)
                ca_loop = 0
            elif which_timeline == "Civilizations":
                print(timelines.TimeCi)
                ca_loop = 0
            elif which_timeline == "War/Conflicts" or "War" or "Conflicts":
                print(timelines.TimeWC)
                ca_loop = 0
            elif which_timeline == "Economics":
                print(timelines.TimeEc)
                ca_loop = 0
            elif which_timeline == "Law" or "Justice System" or "Justice Systems" or "Justice System(s)" or "Law/Justice System(s)" or "Law/Justice Systems":
                print(timelines.TimeLa)
                ca_loop = 0
            elif which_timeline == "doggos":
                ca_loop = 0
            else:
                print("~|Improper Input|~\n")
                which_timeline = "doggos"
        elif turn_action == "finTurn":
            continue
        elif turn_action == "help":
            print(timelines.Help)
            ca_loop = 0
        elif turn_action == "cont":
            ca_loop = 0
            print("blah")
        else:
            print("~|Improper Input|~")
def turn1( tc ):
    t = t + tc
    global turn_action
    while ( loop == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 ):
        print("The current amount of " + time_change + "s is " + str(t))
        turn_action = input("~| ")
        checkAction()
        print("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n")

loopMenu = 1
while( loopMenu == 1 ):
    yn = input("\t\t\t\t\t~|~~~~~~~~~~~~~~~~~~~~~~~~~~|~\n\t\t\t\t\tWelcome to Universe Simulation,\n\t\t\t\t\tthis is an interpretive program\n\t\t\t\t\tthat simulates a universe to a\n\t\t\t\t\tsmall scale. Do you wish to\n\t\t\t\t\tcontinue?\n\t\t\t\t\t\t\t")
    if yn == "Yes":
        print("\n~|Proceeding to Program...")
        loopMenu = 0
    elif yn == "No":
        print("\n~|Closing Porgram...")
        exit()
    else:
        print("\n~|Improper Input: Use Proper Capitalization // Yes or No only Applicable.|~\n")

loop_select = 1
while ( loop_select == 1 ):
    loop = int(input("~|Choose a time-change per turn|~\n~|Applicable Forms of Input: 1 - Milisecond, 2 - Second, 3 - Minute,|~\n~|4 - Hour, 5 - Day, 6 - Month, 7 - Year , and 8 - Cosmic Second|~\n\n~| "))
    if loop == 1:
        print("The time-change is set to miliseconds.\n\n")
        time_change = 'Milisecond'
        loop_select = 0
    elif loop == 2:
        print("The time-change is set to seconds.\n\n")
        time_change = 'Second'
        loop_select = 0
    elif loop == 3:
        print("The time-change is set to minutes.\n\n")
        time_change = 'Minute'
        loop_select = 0
    elif loop == 4:
        print("The time-change is set to hours.\n\n")
        time_change = 'Hour'
        loop_select = 0
    elif loop == 5:
        print("The time-change is set to days.\n\n")
        time_change = 'Day'
        loop_select = 0
    elif loop == 6:
        print("The time-change is set to months.\n\n")
        time_change = 'Month'
        loop_select = 0
    elif loop == 7:
        print("The time-change is set to years.\n\n")
        time_change = 'Year'
        loop_select = 0
    elif loop == 8:
        print("The time-change is set to cosmic seconds.(a really long time)")

        time_change = 'Cosmic Second'
        loop_select = 0
    else:
        print("Input is currently inapplicable, try again.\n")

if loop == 1:
    turn1( 0.1 )
elif loop == 2 or 3 or 4 or 5 or 6 or 7 or 8:
    turn1( 1 )

Tags: ortoloopwhichtimeischangeselect

热门问题