I need the program to ask the user whether they want to choose lambda or k as a parameter, then after I need it to ask for min and max values of k or lambda, depending on what they chose.
here is my code so far;
#ask user if they want a fixed value of k or lambda
def get_klambda():
print("Do you wish to plot curves which vary in:")
print("Shape parameter (k) or scale parameter (lambda)?")
global klambda
lamda = -1
k = -1
while True:
klambda = input("Please select ")
if klambda == "k":
k = float(input("Please enter a scale parameter for lambda: "))
if k >= 0:
break
print("The scale parameter for lambda must be greater than 0!")
def get_kmin(): #ask for min k value
global kmin
kmin = input("Enter minimum k value: ")
if kmin >= 0:
break
print("The minimum k value must be greater than 0!")
get_kmin()
def get_kmax(): #ask for max k value
global kmax
kmax = input("Enter maximum k value: ")
if kmax <= kmin:
break
print("The maximum k value must be greater than the minimum k value!")
get_kmax()
elif klambda == "lambda":
lamda = float(input("Please enter a shape parameter for k: "))
if lamda >= 0:
break
print("The scale parameter for lambda must be greater than 0!")
def get_lmin(): #asl for min lambda value
global lmin
lmin = input("Enter minimum lambda value: ")
if lmin >= 0:
break
print("The minimum lambda value must be greater than 0!")
get_lmin()
def get_lmax():
global lmax
lmax = input("Enter maximum lambda value: ")
if lmax <=lmin:
break
print("The maximum lambda value must be greater than the minimum lambda value!")
get_lmax()
else:
print("Please enter either (k) or (lambda)")
return klambda, k ,lamda, kmin, lmin
klamda, k, lamda, kmin, lmin = get_klambda()
I'm assuming in order for this to work, you will need a function within a function because the questions prompted after asking if they want to choose k or lambda is dependant on their choice.
ie. Do you wish to choose lambda or k? - k
enter shape parameter for lambda - 2
enter minimum k value - 2
ie.2 do you wish to choose lambda or k? - lambda
enter shape parameter for k - 1
enter minimum lambda value - 2
This is the error I'm running into when I run the code:
UnboundLocalError Traceback (most recent call last)
<ipython-input-27-8579ccfa8ebb> in <module>
94 return klambda, k ,lamda, kmin, lmin
95
---> 96 klamda, k, lamda, kmin, lmin = get_klambda()
<ipython-input-27-8579ccfa8ebb> in get_klambda()
92 else:
93 print("Please enter either (k) or (lambda)")
---> 94 return klambda, k ,lamda, kmin, lmin
95
96 klamda, k, lamda, kmin, lmin = get_klambda()
UnboundLocalError: local variable 'kmin' referenced before assignment
kminis defined only ifklambda == "k".kvalues are only being set if you selectkbut you're trying to return them anyway. Same for yourlambdavalues.. If it were me, I'd define two functions, one to get all the k values and one to get all the lambda values (define them outside of theget_klambda()function) and then only call the one I want from within the function