有无大佬debug,不清楚哪里出错了

###python语法不太熟,不知这样写对不对
运行程序的时候最后输入end总是报错,说end没有定义,但我自己并看不出问题

n = str(input("Enter a number, end with 'end':"))
s = []
while n.lower() != "end":
    s.append(int(n))
    n = str(input("Enter a number, end with 'end':"))

s.sort()
even_number = 0
odd_number = 0
for i in s:
    if s[i]%2 == 0:
        even_number+=1
    else:
        odd_number+=1

print('the smallest value is ',s[0],'the largest value is ',s[-1])
print('the number of even number is ',even_number,'the number of odd number is ',odd_number)
Sum = 0
for i in s:
    Sum +=s[i]
    print(Sum)

1个回答

python2的help:

Help on built-in function input in module builtin:

input(...)
input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).

把输入的字符串当成表达式来计算,因此end会解释为一个变量。但是因为end在代码中未定义,所以出错:
NameError: name 'end' is not defined

解决办法: 改用 raw_input()

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!