I want to input enter to stop my program, but ValueError: invalid literal for int() with base 10:" " just appeared

I want to input enter to stop my program, but ValueError: invalid literal for int() with base 10:" " just appeared

我想构建一个可以不断输入标记并将其添加到列表中的圆圈,直到我输入 "Enter"(表示“”),这部分程序将中断并继续。但我坚持在这部分,我搜索了 ValueError,但它似乎与我的情况不符,或者我只是不明白这一点。因此,我在这里问,请帮忙。


#display list
def dis_score():
    for item in score:
        print(item,end = " ")
    print()

#mainbody
score = []
while True:
    x = int(input("Enter the marks please:"))
    if (x>0):
        score.append(x)
    if (x == ""):
        break

print("before sorted:", end = " ")
dis_score()
n = len(score)-1

for i in range(0,n):
    for j in range(0,n-i):
        if (score[j]>score[j+1]):
            score[j],score[j+1]=score[j+1],score[j]
print("sorted:", end = " ")
dis_score()

这是我输入的结果:
请输入分数:80
请输入分数:70
请输入分数:85
请输入分数:

ValueErrorTraceback (most recent call last)  
<ipython-input-5-fa9f906bfcf8> in <module>()  
9 score = []  
10 while True:  
---> 11 x = int(input("Enter the marks please:"))  
12 if (x>0):  
13 score.append(x)  

ValueError: invalid literal for int() with base 10: ''

问题是,如果输入值不是有效整数,int(input("Enter the marks please:")) 将始终引发异常(您正在报告的异常)。

您可以做的是:

while True:
    s = input("Enter the marks please:")
    if s == "":
        break
    x = int(s)
    if x > 0:
        score.append(x)