I am new to python and I would like to learn how to create a function to ask the user what action they want to take and return the action the user selects. I would also want to use a for loop to ask the user for the input 3 times. Thank you in advance.
Asked
Active
Viewed 234 times
-2
-
This sounds like it might be similar to https://stackoverflow.com/questions/72264316/while-iterating-through-my-dictionary-its-not-picking-up-my-keys-even-though-i/72264359#72264359 – Samwise May 17 '22 at 22:58
-
`def my_function(variable):` will define a function called `my_function` and allow you to pass a value to it. You can use `return some_other_variable` (where some_other_variable) is defined in my_function to return that value. Looks like you will want a for loop outside of you function that will be calling that function. `for i in range(3): x = my_function(value)`. – Shmack May 17 '22 at 23:02
1 Answers
1
You can do it like the following, I added comments to explain what each line does.
def func(): # Function decleration
action = input("Enter action") # Get action from user
return action # Print action back to user
for i in range(3): # Loop for 3 times
action_returned = func() ## Get action by calling func
# do something with action

gsan
- 549
- 1
- 4
- 14