简体   繁体   English

不同的输出取决于对多个问题的回答(Python)

[英]Different outputs depending on responses to multiple questions (Python)

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).我试图让代码要求用户选择 k 或 lambda,然后代码需要询问 lambda 的比例参数(如果选择了 k)或 k 的形状参数(如果选择了 Z945F3FC429518A73B9CZF4429518A73B9CZF5) 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. k(或 lambda)的值和增量取决于他们最初选择的内容。

for example, the output would go:例如,output 将是 go:

Would you like to plot curves which vary in: Shape parameter (k) or scale parameter (lambda)?您想要 plot 曲线变化:形状参数 (k) 或比例参数 (lambda)?

Please Choose: (user input - k)请选择:(用户输入 - k)

Please enter a scale parameter for lambda: (user input - 2)请输入 lambda 的比例参数:(用户输入 - 2)

Please enter the max.请输入最大值。 value, min.值,最小值value and increments of k: k的值和增量:

maximum k value (>0): (user input - 1)最大 k 值(>0):(用户输入 - 1)

minimum k value (>min. k value): (user input - 3)最小 k 值(>min. k 值):(用户输入 - 3)

increments of k (between min. and max. value): (user input - 2) k 的增量(在最小值和最大值之间):(用户输入 - 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:..在 k<=0:.. 之后,我不断收到语法错误(ValueError 除外:)

I am using Python 3 Please help.我正在使用 Python 3 请帮忙。

I'm assuming from your description that you want EITHER k or lambda, so the user doesn't need to specify both.我从您的描述中假设您想要 k 或 lambda,因此用户不需要同时指定两者。 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.只需使用if语句。

In this case, removing the exceptions also means you won't catch invalid inputs, like if the user supplies the value "cat" for k .在这种情况下,删除异常也意味着您不会捕获无效输入,例如用户为k提供值“cat”。 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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM