简体   繁体   English

如何从用户输入中读取多个(可变长度)参数并将其存储在变量中并将其传递给函数

[英]How can I read multiple (variable length) arguments from user input and store in variables and pass it to functions

Trying to create a calculator, which can take variable length of integers separated by space.试图创建一个计算器,它可以采用由空格分隔的可变长度的整数。 I am able to create a basic calculator that would read two args and do operations.我能够创建一个基本的计算器,它可以读取两个参数并执行操作。 Below is what I am trying to achieve.以下是我正在努力实现的目标。

select operation: Add
Enter nos: 1 65 12 (this length can increase and any variable lenght of integers can be given)

I am not sure how would I pass this variable length of int to functions, suppose addition function.我不确定如何将这个可变长度的 int 传递给函数,假设加法函数。 I can do it for two variables.我可以为两个变量做到这一点。

Adding what I am aware of:添加我所知道的:

x = input("enter operation to perform")
a = input("1st no.")
b = input("2nd no.")
def add(a,b):
    return a+b
if x == 1:
    print add(a,b)

Not sure how can I pass multiple args read from input to function.不知道如何将多个从输入读取的参数传递给函数。

Using input you can achieve this:使用输入可以实现:

>>> result = input("enter your numbers ")
enter your numbers 4 5
>>> result
'4 5'
>>> a, b = result.split()
>>> a
'4'
>>> b
'5'
>>> int(a) + int(b)
9

The split method will split your string by default on space and create a list of those items. split方法将默认在空间上拆分您的字符串并创建这些项目的列表。

Now, if you had something more complicated like:现在,如果你有更复杂的东西,比如:

>>> result = input("enter your numbers ")
enter your numbers 4 5 6 7 8 3 4 5
>>> result
'4 5 6 7 8 3 4 5'
>>> numbers = result.split()
>>> numbers
['4', '5', '6', '7', '8', '3', '4', '5']
>>> numbers = list(map(int, numbers))
>>> numbers
[4, 5, 6, 7, 8, 3, 4, 5]
>>> def add(numbers):
...  return sum(numbers)
...
>>> add(numbers)
42

As you can see you are taking a longer sequence of numbers split by space.正如您所看到的,您正在使用由空格分割的更长的数字序列。 When you call split on it, you will see you have a list of numbers but represented as strings.当你调用split时,你会看到你有一个数字列表,但表示为字符串。 You need to have integers.你需要有整数。 So, this is where the call to map comes in to type the strings to integers.因此,这就是调用map以将字符串键入为整数的地方。 Since map returns a map object, we need a list (hence call to list around the map).由于 map 返回一个 map 对象,我们需要一个列表(因此调用 list 周围的地图)。 Now we have a list of integers, and our newly created add function takes a list of numbers, and we simply call sum on it.现在我们有一个整数列表,我们新创建的add函数接受一个数字列表,我们只需对其调用sum

If we wanted something that required a little more work, like subtraction, as suggested.如果我们想要一些需要更多工作的东西,比如减法,就像建议的那样。 Let us assume we already have our list of numbers, to make the example smaller to look at:让我们假设我们已经有了我们的数字列表,为了让示例看起来更小:

Furthermore, to help make it more readable I will do it step by step:此外,为了使其更具可读性,我将逐步进行:

>>> def sub(numbers):
...  res = 0
...  for n in numbers:
...   res -= n
...  return res
...
>>> sub([1, 2, 3, 4, 5, 6, 7])
-28

If you use *args it can take an arbitrary amount of positional arguments.如果您使用 *args,它可以采用任意数量的位置参数。 You can make similar procedures for other operations.您可以对其他操作进行类似的程序。

def addition(*args):
    return sum(args)

calc = {
        '+':addition,
        #'-':subtraction,
        #'/':division,
        #'*':multiplication,
        }

def get_input():
    print('Please enter numbers with a space seperation...')
    values = input()
    listOfValues = [int(x) for x in values.split()]
    return listOfValues


val_list = get_input()

print(calc['+'](*val_list))

This is the way I would implement the calculator.这就是我实现计算器的方式。 Have a dictionary that holds the operations (I would use lambdas), then you can pass the list of numbers to the specific operation in the dictionary.有一个保存操作的字典(我会使用 lambdas),然后您可以将数字列表传递给字典中的特定操作。

暂无
暂无

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

相关问题 如何使用用户输入将特定变量从文件传递到另一个变量 - How can I pass a specific variable from a file to another variable with user input 如何允许用户根据用户输入在程序中存储多个 CSV 文件? - Python - How can I allow the user to store multiple CSV files in a program from user input? - Python 如何将可变长度参数传递给另一个函数的参数? - How do I pass variable length arguments to parameters of another function? 如何在 python 中将多个文件作为输入并存储在变量中 - How can I take multiple file as input in python & store in a variable 如何将原始输入存储到变量中,然后将该变量传递到另一个脚本中而又无需再次请求输入? - How can I store raw input into a variable and then pass that variable into another script without requesting input again? 如何将多个用户输入值从 PySimpleGUI 传递给函数,然后将结果输出到 PySimpleGUI? - How can I pass multiple user input values from PySimpleGUI to a function, then output result to PySimpleGUI? 如何读取用户命令输入并将零件存储在变量中 - How to read user command input and store parts in variables 是否可以以及如何将参数传递给聚合中使用的多个函数? - Can and how do I pass arguments to multiple functions used within an aggregation? 如何将列表中的每个值存储到多个变量? - How can I store each value from a list to multiple variables? 如何存储要由多个函数使用的相同参数 - How do I store identical arguments to be used by multiple functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM