Python列表读取整数

问题描述:

我想创建一个简单的脚本,应该计算列表中有多少个integersstrings。 起初列表为空,则该用户被要求用数字或字符串来填补它,这是脚本:Python列表读取整数

lis = []   # list name 

num, lett = 0, 0 # init counters of numbers and letters 
while True: 
    x = input("digit an int or string, 'stop' returns values: ") 
    if(x=='stop'): 
     False 
     break 

    i = lis.append(x) 
    if isinstance(i, int): # check whether is integer 
     num += 1 
    else: 
     lett += 1  

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 

该项目工程,但问题就来了,最终,因为当我打印列表,它只能看到字符串,偶数例如返回为'10'。

我需要解释器来自动识别整数。

+0

你需要使用'X .isdigit()',因为'x'仍然是一个字符串'isinstance()'每次都会失败,因为它测试变量的数据类型而不是_character content_。但是除了这个之外,你的代码中还有很多更多的错误。我建议阅读Python的教程,以了解如何在其中进行编程。 –

+0

''程序工作''我非常怀疑这一点。 ''lis.append(x)''returns''None''。顺便说一句:在“break”之前的“False”什么都不做。 –

+0

**正在回答**的用户:请停止回答此问题。它是广泛的,无关紧要的,很可能不会对未来的用户有所帮助。如果他阅读一个Python的begginer教程,它将使OP受益匪浅。在代码转储完成后,只需发布​​代码转储无助于任何人。至少你可以提供你的代码的解释。 –

lis = []   # list name 

num, lett = 0, 0 # init counters of numbers and letters 
while True: 
    x = input("digit an int or string, 'stop' returns values: ") 
    if(x=='stop'): 
    break 
    if x.isdigit(): 
    lis.append(int(x)) 
    num += 1 
    elif x.isalpha(): 
    lis.append(x) 
    lett += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 

结果

digit an int or string, 'stop' returns values: 1 
digit an int or string, 'stop' returns values: 2 
digit an int or string, 'stop' returns values: 3 
digit an int or string, 'stop' returns values: a 
digit an int or string, 'stop' returns values: b 
digit an int or string, 'stop' returns values: c 
digit an int or string, 'stop' returns values: stop 
[1, 2, 3, 'a', 'b', 'c'] 
there are 3 numbers 
there are 3 strings 

d = l = 0 
res = [] 
while True: 
    s = input("input string or digit\n") 
    if s == 'exit': 
     break 

    ## this is one way, might be faster to do it using isdigit suggested in the comment 
    try:    
     temp = int(s) 
     d += 1 
    except ValueError: 
     l += 1 
    res.append(s) 

print(d) 
print(l) 
print(res) 
+0

使用裸“异常”条款被认为是不好的做法。改为使用特定的例外。 –

您正在检查list.append的返回值是否为整数,而不是。因此,它将其视为一个字符串。

lis = []   # list name 

num, lett = 0, 0 # init counters of numbers and letters 
while True: 
    x = input("digit an int or string, 'stop' returns values: ") 
    lis.append(x) 
    if(x=='stop'): 
     break 

for items in lis: 
    if items.isdigit(): # check whether is integer 
     num += 1 
    else: 
     lett += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 

这里是我的解决方案,它在我的Python IDLE版本3.6.0工程(请回复,如果它不为你工作):

lis = []   # list name 

num, lett = 0, 0 # init counters of numbers and letters 

while True: 
    try: 
     x = input("digit an int or string, 'stop' returns values: ") 
     i = lis.append(x) 
     if(x=='stop'): 
      False 
      break 
    except ValueError: 
     print("Not an integer!") 
     continue 
    else: 
     num += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 
+0

目前它不识别字符串,每个值都被视为整数,因为它只会增加_num_,即使我写'蛋' –

这里是我的代码

while True: 
    l,num,lett = [],0,0 
    while True: 
     x = input('digit an int or string, "stop" returns values: ').lower().strip() 
     try: 
      x = int(x) 
     except: 
      pass 
     if x == ('stop'): 
      break 
     l.append(x) 

    for element in l: 
     if isinstance(element, int): 
      num += 1 
     else: 
      lett += 1 

    print (l) 
    print ("there are " + str(num) + " numbers") 
    print ("there are " + str(lett) + " strings") 
    l,num,lett = [],0,0  #reset to go again 

您可以使用isdigit和你的代码改成这样:

lis = []   # list name 

num, lett = 0, 0 # init counters of numbers and letters 
while True: 
    x = input("digit an int or string, 'stop' returns values: ") 
    if(x=='stop'): 
     False 
     break 
    if x.isdigit(): # check whether is integer 
     lis.append(int(x)) 
     num += 1 
    else: 
     lis.append(x) 
     lett += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 

而且,你可以用这个解决您的代码(它需要一个缩进,并将其移动到主while):

if isinstance(int(x), int): # check whether is integer 
    num += 1 
    lis.append(int(x)) 
else: 
    lett += 1 
    lis.append(x) 
+0

好吧,它确实有用,谢谢。还有一个问题:当我打印列表时,我看到这个输出['value1','value2','value3']。 这意味着列表中充满了字符串,不是吗? 如果value1是一个整数,输出是[value1,'value2','value3'],还是不是? –

+0

@JakCooper是的!我更新了我的答案;) – RaminNietzsche