Home > Software engineering >  How to write a function to return user input and calculate
How to write a function to return user input and calculate

Time:05-18

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.

CodePudding user response:

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
  • Related