我想要建立一个圆圈,我可以继续输入标记,并将其添加到列表中,直到输入"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: ''发布于 2018-09-18 17:33:34
问题是,如果输入值不是有效整数,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)https://stackoverflow.com/questions/52391839
复制相似问题