I'm trying to write a program where test scores are collected in lists and then certain factors such as highest scores are outputted. However when I try to assign intH1 (highest result for test 1) I get the above error. The line is intH1 = score1_list[intCount] and strHN1 = name_list[intCount]
if score1_list[intCount] > intH1:
intH1 = score1_list[intCount] and strHN1 = name_list[intCount]
if score2_list[intCount] > intH2:
intH2 = score2_list[intCount] and strHN2 = name_list[intCount]
if score3_list[intCount] > intH3:
intH3 = score3_list[intCount] and strHN3 = name_list[intCount]
if total_list[intCount] > intHT:
intHT = total_list[intCount] and strHNT = name_list[intCount]`
You cannot use and
to assign two variables. Python parses your assignment as:
intH1 = (score1_list[intCount] and strHN1) = name_list[intCount]
trying to assign the result of the name_list[intCount]
expression to both intH1
and to score1_list[intCount] and strHN1
. and
is an operator, which can only be used in expressions , but assignment is a statement . Statements can contain expressions, expressions cannot contain statements.
This is why the defined grammar for assignments uses the grammar entities *expression_list and
yield_expression , two expression forms you can use, only in the part to the right of the
=` equals sign:
assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression)
while the target_list
definition doesn't allow any use of arbitrary expressions.
Use separate lines for assignment:
intH1 = score1_list[intCount]
strHN1 = name_list[intCount]
or use tuple assignment:
intH1, strHN1 = score1_list[intCount], name_list[intCount]
Each branch of the if
does two assignments. You don't need an and
between them, you just need to separate them to two statements:
if score1_list[intCount] > intH1:
intH1 = score1_list[intCount]
strHN1 = name_list[intCount]
if score2_list[intCount] > intH2:
intH2 = score2_list[intCount]
strHN2 = name_list[intCount]
if score3_list[intCount] > intH3:
intH3 = score3_list[intCount]
strHN3 = name_list[intCount]
if total_list[intCount] > intHT:
intHT = total_list[intCount]
strHNT = name_list[intCount]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.