0

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 e.g user want to run function 1 and 3 (i.e. 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.

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

That was a real quick solution, Thank you :). one more thing can I take values separated by comma instead space ?
0

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

2 Comments

Thanks Varnit, I have marked moghya answer as solution but your split(",") was also helpful. I am not able to give upvote to your answer as my account is new.
No that's not possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.