简体   繁体   中英

Calling Multiple Functions based on user selection in Python

I'm using Python version 3. I want to call a function based on the user input and the below code works fine.

def askUser():
    while True:
        try:
            choice = int(input("Do you want to: \n(1) Run f1 \n(2) Run f2 \n(3) Run f3 \n(4) Run f4 \n"))
        except ValueError:
            print("Please input a number")
            continue
        if 0 < choice < 5:
            break
        else:
            print("That is not between 1 and 4! Try again:")
    print ("You entered: {} ".format(choice))

    def f1():
        print("f1 was called")
    def f2():
        print("f2 was called")
    def f3():
        print("f3 was called")
    def f4():
        print("f4 was called")
    mydict = {1:f1, 2:f2, 3:f3, 4:f4}
    mydict[choice]()

askUser()

Now the issue is that I want to take multiple values from user.For eg user want to run function 1 and 3 (ie first function 1 and then 3) , so he will enter values separated by 1,3 and will hit enter, which will trigger f1 and then f3.

How can I achieve this?

Multiple values need not necessarily be separated by comma but user should be able to give all values in one go and then all function should run in same sequence.

Take choices in a list, see if below code works for you.

def askUser():
    def f1():
        print("f1 was called")
    def f2():
        print("f2 was called")
    def f3():
        print("f3 was called")
    def f4():
        print("f4 was called")
    mydict = {1:f1, 2:f2, 3:f3, 4:f4}

    while True:
        try:
            choices = list(map(int,input("Do you want to: \n(1) Run f1 \n(2) Run f2 \n(3) Run f3 \n(4) Run f4 \n").split()))
        except ValueError:
            print("Please input number")
            continue
        for choice in choices:
            if 0 < choice and choice < 5:
                mydict[choice]()
            else:
                print("That is not between 1 and 4! Try again:")




askUser()

Following is the output

sawant@sawant:~$ python sol.py 
Do you want to: 
(1) Run f1 
(2) Run f2 
(3) Run f3 
(4) Run f4 
1 2 3
f1 was called
f2 was called
f3 was called
Do you want to: 
(1) Run f1 
(2) Run f2 
(3) Run f3 
(4) Run f4 
4 3 2
f4 was called
f3 was called
f2 was called
Do you want to: 
(1) Run f1 
(2) Run f2 
(3) Run f3 
(4) Run f4 
1 5
f1 was called
That is not between 1 and 4! Try again:
Do you want to: 
(1) Run f1 
(2) Run f2 
(3) Run f3 
(4) Run f4 
1 9 2 
f1 was called
That is not between 1 and 4! Try again:
f2 was called
Do you want to: 
(1) Run f1 
(2) Run f2 
(3) Run f3 
(4) Run f4 
1 .
Please input number
Do you want to: 
(1) Run f1 
(2) Run f2 
(3) Run f3 
(4) Run f4 

you can do that using data.split function and then run a loop around

data = input.split(",") #split string into a list
for temp in data:
   mydict[temp]();

let me know it that helps

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