简体   繁体   English

如何编写一个函数来返回用户输入并计算

[英]How to write a function to return user input and calculate

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.我是 python 新手,我想学习如何创建一个函数来询问用户他们想要采取什么操作并返回用户选择的操作。 I would also want to use a for loop to ask the user for the input 3 times.我还想使用 for 循环向用户询问输入 3 次。 Thank you in advance.先感谢您。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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