簡體   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