简体   繁体   中英

functions within functions python

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

This will collect the data needed:

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":
            while True:
                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!")

            while True:
                kmin = float(input("Please enter the value of kmin: "))
                if k >= 0:
                    break
                print("The minimum k value must be greater than 0!")

            lmin = -1
            Lambda = -1

        elif klambda == "lambda":
            while True:
                Lambda = float(input("Please enter a shape parameter for k: "))
                if Lambda >= 0:
                    break
                print("The shape parameter for k must be greater than 0!")

            while True:
                lmin = float(input("Please enter the value of lmin: "))
                if k >= 0:
                    break
                print("The minimum k value must be greater than 0!")

            kmin = -1
            k = -1
        else:
            print("Please enter either (k) or (lambda)")
        break

    return klambda, k ,Lambda, kmin, lmin

klamda, k, Lambda, kmin, lmin = get_klambda()

print(klambda, k, Lambda, kmin, lmin)

you don't need nested functions just a while loop for each selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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