繁体   English   中英

如何将数字放入列表中,但如何在用户输入中添加“,”?

[英]How can I put numbers in a list but add “, ” to the user input?

我想使用python快速过滤我不在乎的事件查看器错误代码。 我想将错误代码输入到列表中,然后python打印列表并循环执行相同的输入查询,直到input = end ...这是我想要的输出:

Input error code: 1103
1103
Input error code: 736
1103
736
Input error code: 235
1103
736
235
Input error code: end
1103, 736, 235

因此,我可以轻松地将错误代码粘贴到过滤器中,而无需手动逗号和空格!

这就是我尝试过的

n = input("Input error code: ")

def mylist (n):
    codes = []
    while True:
        for n in codes(input("Input error code: ")):
            return (codes) + (", ")
        else:
            n in codes == "end"
            break

print (mylist(n))

我真的很喜欢这个东西,并且已经尝试和研究了数小时的stackoverflow,请帮忙! 运行时,我不断收到此错误:

一些回溯错误,然后

for n in codes(input("Input error code: ")):
TypeError: 'list' object is not callable

任何帮助将是巨大的!

希望这个能对您有所帮助

urbanecm@notebook ~ 
$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list = ['melon', 'apple', 'juice']
>>> print(", ".join(list))
melon, apple, juice
>>> 
my_list = []
n = input("Input error code: ")
while n != 'end':
  my_list.append(n)
  print(*my_list, sep='\n')
  n = input("Input error code: ")
print(*my_list, sep=', ')

此代码适用于您的用例:

error_codes = []
finish = False

while finish is False:
    code = raw_input("Input error code: ")
    if code == "end":
        finish = True
    else:
        error_codes.append(code)

print(", ".join(error_codes))

输出:

Input error code: 12
Input error code: 43
Input error code: 567
Input error code: end
12, 43, 567

注意! 如果您使用python 3.x,请使用input代替raw_input

暂无
暂无

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

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