def main():
i = int( input ("Enter an interger, the input ends if it is 0: "))
count_pos = 0
count_neg = 0
total = 0
if (i != 0):
while (i != 0):
if (i > 0):
count_pos += 1
elif (i < 0):
count_neg += 1
total += i
i = int( input ("Enter an interger, the input ends if it is 0: "))
count = count_pos + count_neg
print ("The number of positives is", count_pos)
print ("The number of negatives is", count_neg)
print ("The total is", total)
文件&#34;&#34;,第16行 打印(&#34;肯定数是&#34;,count_pos) ^ IndentationError:unindent与任何外部缩进级别都不匹配
答案 0 :(得分:0)
请参阅我们的列表,例如sample_list
sample_list = [1, 5, -9, 6, 7, -7, -2, -4, 2, 3]
或者
sample_list = []
while True:
a = int(raw_input())
if a==0: break
else:sample_list.append(a)
现在,要获得列表的长度
sample_list_length = len(sample_list)
其中len()是一个内置函数,它返回任何可迭代对象的长度,如字符串,列表等。
positive_number = 0
negative_number = 0
for dummy_number in sample_list:
if dummy_number>=0:
positive_number+=1
else:
negative_number+=1
print "There are",positive_number,"positive numbers"
print "There are",negative_number,"negative numbers"
print "There are",sample_list_length,"total numbers"
答案 1 :(得分:0)
您的代码存在一些问题。
首先,
while (i != 0):
if (i > 0):
count_pos += 1
elif (i < 0):
count_neg += 1
total += i
是一个无限循环。 i
永远不会更改,因此如果第一次i!=0
为True,则它始终为True。
接下来,
i = int( raw_input ("Enter an interger, the input ends if it is 0: "))
count = count_pos + count_neg
完全没必要。以下代码中均未使用i
而非count
。
固定代码:
def main():
count_pos = 0
count_neg = 0
while True:
i = int( input ("Enter an interger, the input ends if it is 0: "))
if i > 0:
count_pos += 1
elif i < 0:
count_neg += 1
else:
break
print ("The number of positives is", count_pos)
print ("The number of negatives is", count_neg)
print ("The total is", count_pos+count_neg)