I am trying to get the code to ask the user to choose either k or lambda and then the code needs to ask for a scale parameter for lambda (if k was chosen) or a shape parameter for k (if lambda was chosen). From here, I need the code to ask the user for an input to get min. value, max. value and increments of k (or lambda) depending on what they chose originally.
for example, the output would go:
Would you like to plot curves which vary in: Shape parameter (k) or scale parameter (lambda)?
Please Choose: (user input – k)
Please enter a scale parameter for lambda: (user input – 2)
Please enter the max. value, min. value and increments of k:
maximum k value (>0): (user input – 1)
minimum k value (>min. k value): (user input – 3)
increments of k (between min. and max. value): (user input – 2)
This is what I’ve gotten so far
#ask user if they want a fixed value of k or lambda
print("Do you wish to plot curves which vary in:")
print("Shape parameter (k) or scale parameter (lambda)?")
def get_klambda():
global klambda
while True:
try:
klambda = str(input("Please select "))
if (klambda =="k"):
k = input("Please enter a scale parameter for lambda: ")
if k <=0:
break
raise ValueError()
except ValueError:
print("The scale parameter for lambda must be greater than 0!")
break
if (klambda=="lambda"):
lamda = input("Please enter a shape parameter for k: ")
if lamda <=0:
break
raise ValueError()
except ValueError:
print("The shape parameter for k must be greater than 0!")
break
raise ValueError()
except ValueError:
print("Please enter either (k) or (lambda)")
get_klambda()
#get min, max and inc of k
if (klambda == "k"):
print("Please enter the min. value, max. value and increment for the shape parameter (K) ")
def get_kmin():
global kmin
while True:
try:
kmin = float(input("Please enter minimum k value: "))
if kmin >=0.1:
break
raise ValueError()
except ValueError:
print("The minimum k value must be greater than 0!")
get_kmin()
I keep getting syntax error where it says (except ValueError:) after k<=0:..
I am using Python 3
Please help.
>Solution :
I’m assuming from your description that you want EITHER k or lambda, so the user doesn’t need to specify both. That would be done like this. Note that I have removed your raising of exceptions. When you are doing your own validation, don’t use exceptions. Just use if statements.
In this case, removing the exceptions also means you won’t catch invalid inputs, like if the user supplies the value "cat" for k. Whether you need to catch that depends on the sophistication of your users. If they were trying to break your code, well maybe they deserve to see the exception.
#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)?")
lamda = -1
k = -1
while True:
klambda = input("Please select ")
if klambda == "k":
k = int(input("Please enter a scale parameter for lambda: "))
if k > 0:
break
print("The scale parameter for lambda must be greater than 0!")
elif klambda == "lambda":
lamda = int(input("Please enter a shape parameter for k: "))
if lamda > 0:
break
print("The shape parameter for k must be greater than 0!")
else:
print("Please enter either (k) or (lambda)")
return klambda, k, lamda
klamda, k, lamda = get_klambda()