简体   繁体   中英

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

I want to use python to quickly filter event viewer error codes I don't care about. I want to input an error code to a list and python prints the list and loops the same input inquiry until input = end... This is the output I'm trying for:

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

So then I can easily paste the error codes in the filter without having to comma and space manually!

This is what I've tried

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

I really suck at this stuff and have been trying and researching stackoverflow for hours now, please help! I keep getting this error when it runs:

Some Traceback error and then,

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

Any help would be great!

Hope this will help you

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=', ')

This code should work for your use case:

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

Output:

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

Note! If you use python 3.x use input instead of raw_input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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