[英]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.