[英]Need explanation for why my code produces a ValueError
I am very new to python and have been struggling with this issue for quite some time now. 我是python的新手,并且已经为这个问题苦苦挣扎了一段时间。 I don't understand that why is my code producing this error message and abruptly stops in between: 我不明白为什么我的代码会产生此错误消息并突然在以下两者之间停止:
Traceback (most recent call last):
File "C:/...some path../Random_marks.py", line 70, in <module>
total_marks = int(total)
ValueError: invalid literal for int() with base 10: 'A\n'
From what I understand is that it is never going in the if
statement in line number 66 (marked with a comment below) of my code. 据我了解,它永远不会出现在我的代码第66行的if
语句(在下面带有注释)中。 But I don't understand why and how to fix it? 但是我不明白为什么以及如何解决它? Variable total is of string type only, so isalpha()
should return True
. 变量total仅是字符串类型,因此isalpha()
应该返回True
。 But when I print it, it says False
for some reason. 但是当我打印它时,由于某种原因它说False
。
Here is the enitre code, but problems are only in the while loop at the bottom I guess. 这是enitre代码,但我猜问题仅在底部的while循环中。
from random import *
infile = open("Compb12-81test1.txt", "r")
outfile = open("Compb12-81test1_marks.txt", "w")
def alldone(done):
for i in done:
if i == 0:
return False
return True
def assign2m(marks,done,total):
extra = randint(0,5)
done[extra] = 1
while not alldone(done[0:6]):
mark = randint(0,2)
select = randint(0,5)
if total - mark >= 0 and done[select] == 0 and select != extra:
total -= mark
marks[select] = mark
done[select] = 1
return total
def assign5m(marks,done,total):
extra1 = randint(6,7)
extra2 = randint(8,9)
done[extra1] = 1
done[extra2] = 1
while not alldone(done[6:10]):
mark = randint(0,5)
select = randint(6,9)
if total - mark >= 0 and done[select] == 0 and select != extra1 and select != extra2:
total -= mark
marks[select] = mark
done[select] = 1
return total
def adjust(marks,total,questions):
for i in range(questions):
if total > 0:
if i < 6 and str(marks[i]).isdigit():
diff = 2 - marks[i]
if total - diff >= 0:
marks[i] = 2
total -= diff
else:
marks[i] += total
total = 0
elif i < 10 and str(marks[i]).isdigit():
diff = 5 - marks[i]
if total - diff >= 0:
marks[i] = 5
total -= diff
else:
marks[i] += total
total = 0
else:
break
questions = 10
total = str(infile.readline())
while total != '-1':
marks = [" "] * questions
done = [0] * questions
if total.isalpha(): # This is line 66
outfile.write("A," * (questions - 1))
outfile.write("A" + "\n")
else:
total_marks = int(total)
total = assign2m(marks,done,int(total))
if int(total) > 0:
total = assign5m(marks,done,int(total))
if int(total) > 0:
adjust(marks,int(total),questions)
total = str(infile.readline())
print(marks,total_marks)
print(total, type(total))
infile.close()
outfile.close()
And this is my input txt file content: 这是我输入的txt文件的内容:
12
2
13
12
9
16
10
6
20
8
6
5
10
5
13
5
9
5
14
14
8
8
9
9
13
A
10
9
10
12
18
13
9
20
16
14
7
3
11
5
8
9
17
8
11
12
13
8
8
16
4
8
1
9
13
17
19
10
6
18
9
15
12
5
4
8
8
16
15
7
-1
Problem occurs when it reads the character 'A'
. 读取字符'A'
时会发生问题。
isalpha
checks if every character of the string is alphabetic. isalpha
检查字符串的每个字符是否都是字母。 Since your string contains a newline character \\n
, total.isalpha()
returns False
. 由于您的字符串包含换行符\\n
,因此total.isalpha()
返回False
。 You can strip the whitespace with 您可以使用以下方法删除空格
total = str(infile.readline()).strip()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.