Python:用于计算无限原始输入的函数

2024-10-06 07:51:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我被困在简单的事情,但我不能睡好,直到我要解决这个问题。你知道吗

所以我的想法是:

  1. 只要输入所需的字母(例如x),就列出原始输入
  2. 使用未定义的长度变量生成函数(使用*args)
  3. 对变量(已解析的参数)求和

挑战:

  1. 循环进行符号验证
  2. 使用append(或extend)生成原始输入的列表,但不包括列表中的最后一个符号x
  3. 将正确的类型参数转发给函数
  4. 如果我的论据被塞得满满当当,怎么做一个总结是个谜 元组(*args)?你知道吗

例如,如果我使用append创建一个输入列表,那么在求和时我收到:

k = sum(args) #works if list of (rgs can be readed as ints or strs TypeError: unsupported operand type(s) for +: 'int' and 'str'

一些代码:

def autosum(*args):
k = sum(args) #works if list of (rgs can be readed as ints or strs
print "Your sum", k
return

print ("Type integer or x to close")


input_list = list()
while True:
    rawinputvalue = raw_input(">>")
    input_list = rawinputvalue
    if type(rawinputvalue) != "x":
        break
    input_list.append(rawinputvalue)

autosum(input_list)

这些是如何使用不同类型的参数以及函数的一些用法的基础知识。希望其他人也能从中吸取教训,谢谢。 唐


Tags: orof函数类型列表input参数if