我正在尝试创建一个简单的脚本,该脚本应该计算列表中有多少integers
或strings
。
首先列表为空,然后要求用户用数字或字符串填充它,这是脚本:
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 :(得分:0)
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)
答案 1 :(得分: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")
答案 2 :(得分:0)
这是我的解决方案适用于我的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")
答案 3 :(得分:0)
这是我的代码
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
答案 4 :(得分:0)
您可以使用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)
答案 5 :(得分:0)
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