我试图在我的python上调整子文件,但它一直向我显示先前引用的变量(Min_Alpha)上的unboundlocalerror。我很困惑,如果我运行alpha命令,我没有得到错误,虽然当我运行beta命令,我得到这个错误。如有任何帮助,不胜感激。
def main():
print("Welcome to the Beta Distribution Software")
print(" ")
playerschoice = input("Do you want to use this software ? Yes/No : ")
if (playerschoice == "yes" or playerschoice =="Yes" or playerschoice == "YES"):
Body_function_01()
else:
end_program()
def Body_function_01():
users_choice = input("What constant would you like to vary ? Alpha/Beta: ")
if (users_choice == "Alpha" or users_choice == "alpha"):
Max_Alpha = float(input("Enter the Maximum Alpha Value: "))
Min_Alpha = float(input("Enter the Minimum Alpha Value: "))
Alpha_increment = float(input("Enter Alpha Increment Value: "))
Beta_constant = float(input("Enter constant Beta value"))
else:
Body_function_02()
if (Min_Alpha >= 0):
print("Minimum Alpha value is within an acceptable range")
elif (Min_Alpha < 0):
print("Minimum value of alpha is too low ")
print ("Try again")
if ((Max_Alpha - Min_Alpha) > Alpha_increment):
print("The Alpha Increment is within an acceptable range")
elif ((Max_Alpha - Min_Alpha) < Alpha_increment):
print("The Alpha increment is too high")
print("Try again")
if (Max_Alpha > Min_Alpha):
print("The Maximum value of alpha is within an acceptable range")
elif (Max_Alpha < Min_Alpha):
print("The maximum value of alpha is too high")
print("Try again")
def Body_function_02():
users_choice = input("What constant would you like to vary ? Alpha/Beta: ")
if (users_choice == "Beta" or users_choice == "beta"):
Max_Beta = float(input("Enter the Maximum Beta Value: "))
Min_Beta = float(input("Enter the Minimum Beta Value: "))
Beta_increment = float(input("Enter Beta Increment Value: "))
Alpha_constraint = float(input("Enter constant Alpha value: "))
else:
print("Try again")
if (Min_Beta >= 0):
print("Minimum Beta value is within an acceptable range")
elif (Min_Beta < 0):
print("Minimum value of Beta is too low ")
print ("Try again")
if ((Max_Beta - Min_Beta) > Beta_increment):
print("The Beta Increment is within an acceptable range")
elif ((Max_Beta - Min_Beta) < Beta_increment):
print("The Beta increment is too high")
print("Try again")
if (Max_Beta > Min_Beta):
print("The Maximum value of Beta is within an acceptable range")
elif (Max_Beta < Min_Beta):
print("The maximum value of Beta is too high")
print("Try again")
def end_program():
print(" ")
input("Press any key to leave")
sys.exit()
main()
Body_function_01()
Body_function_02()
end_program().
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-25-c662d0a92b77> in <module>
111
112
--> 113 main()
114 Body_function_01()
115 Body_function_02()
<ipython-input-25-c662d0a92b77> in main()
16 playerschoice = input("Do you want to use this software ? Yes/No : ")
17 if (playerschoice == "yes" or playerschoice =="Yes" or playerschoice == "YES"):
---> 18 Body_function_01()
19
20 else:
<ipython-input-25-c662d0a92b77> in Body_function_01()
33 Body_function_02()
34
---> 35 if (Min_Alpha >= 0):
36 print("Minimum Alpha value is within an acceptable range")
37 elif (Min_Alpha < 0):
UnboundLocalError: local variable 'Min_Alpha' referenced before assignment
我认为问题是Min_Alpha
在以下情况下被初始化:
users_choice = input("What constant would you like to vary ? Alpha/Beta: ")
if (users_choice == "Alpha" or users_choice == "alpha"):
Max_Alpha = float(input("Enter the Maximum Alpha Value: "))
Min_Alpha = float(input("Enter the Minimum Alpha Value: "))
Alpha_increment = float(input("Enter Alpha Increment Value: "))
Beta_constant = float(input("Enter constant Beta value"))
只有当用户选择Alpha/Alpha时才会初始化。对于任何其他选择,变量将不会被初始化,并且当您将其与0进行比较时,它将是无界的。不确定预期的行为是什么,但我认为你应该在Body_function_01
和Body_function_02
函数的else
情况下return
。