如何从用户输入中读取多个(可变长度)参数并将其存储在变量中并将其传递给函数 [英] How can I read multiple (variable length) arguments from user input and store in variables and pass it to functions

查看:36
本文介绍了如何从用户输入中读取多个(可变长度)参数并将其存储在变量中并将其传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个计算器,它可以采用由空格分隔的可变长度的整数.我能够创建一个基本的计算器,它可以读取两个参数并执行操作.以下是我正在努力实现的目标.

选择操作:添加输入 nos: 1 65 12 (这个长度可以增加并且可以给出任何整数的可变长度)

我不确定如何将这个可变长度的 int 传递给函数,假设是加法函数.我可以为两个变量做到这一点.

补充我所知道的:

x = input("输入要执行的操作")a = input("第一个编号")b = input("第二个号码")定义添加(a,b):返回 a+b如果 x == 1:打印添加(a,b)

不知道如何将多个从输入读取的参数传递给函数.

解决方案

使用 input 可以实现:

<预><代码>>>>result = input("请输入您的数字")输入您的号码 4 5>>>结果'4 5'>>>a, b = result.split()>>>一种'4'>>>乙'5'>>>整数(a) + 整数(b)9

split 方法将默认在空间上拆分您的字符串并创建这些项目的列表.

现在,如果你有更复杂的东西:

<预><代码>>>>result = input("请输入您的数字")输入您的数字 4 5 6 7 8 3 4 5>>>结果'4 5 6 7 8 3 4 5'>>>数字 = result.split()>>>数字['4', '5', '6', '7', '8', '3', '4', '5']>>>数字 = 列表(地图(整数,数字))>>>数字[4, 5, 6, 7, 8, 3, 4, 5]>>>定义添加(数字):...返回总和(数字)...>>>添加(数字)42

如您所见,您正在使用由空格分隔的更长的数字序列.当您对其调用 split 时,您会看到您有一个数字列表,但表示为字符串.你需要有整数.因此,这就是调用 map 以将字符串键入为整数的地方.由于 map 返回一个 map 对象,我们需要一个列表(因此调用 list 周围的地图).现在我们有一个整数列表,我们新创建的 add 函数接受一个数字列表,我们只需在它上面调用 sum.

如果我们想要一些需要更多工作的东西,例如减法,如建议的那样.让我们假设我们已经有了我们的数字列表,为了让示例看起来更小:

此外,为了使其更具可读性,我将逐步进行:

<预><代码>>>>定义子(数字):... 资源 = 0... 对于 n 的数字:... res -= n...返回资源...>>>sub([1, 2, 3, 4, 5, 6, 7])-28

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. 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.

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. You need to have integers. So, this is where the call to map comes in to type the strings to integers. Since map returns a map object, we need a list (hence call to list around the map). 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.

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

这篇关于如何从用户输入中读取多个(可变长度)参数并将其存储在变量中并将其传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆