khaled motasem
khaled motasem

Reputation: 23

Why am I receiving the same output regardless of how much my score is?

I'm doing this "quiz" and I'm trying to add like a scoring system


#starting score
score = 0

if Question_1 in ("Cheetah", "CHEETAH", "cheetah"):
  score =+ 1
else:
  pass

if Question_2 in ("Sailfish", "SAILFISH", "sailfish"):
  score =+ 1
else:
  pass

if Question_3 in ("Peregrine Falcon", "peregrine falcon", "PEREGRINE FALCON", "pf"):
  score =+ 1
else:
  pass

if Question_4 in ("A decade", "Decade", "DECADE", "decade"):
  score =+ 1
else:
  pass


if Question_5 in ("Centimetre", "Centimetres", "CENTIMETRE", "CM", "cm", "centi", "centimetre"):
  score =+ 1
else:
  pass


#once all answers have been checked display result
if score == 0:
  print("your result was 0/5, very bad!")
elif score == 1:
  print("your result was 1/5, bad!")
elif score == 2:
  print("your result was 2/5, mid!")
elif score == 3:
  print("your result was 3/5, good!")
elif score == 4:
  print("your result was 4/5, very good!!")
elif score == 5:
  print("your result was 5/5, EXCELLENT!!")
else:
  pass

when I test out my code, if my score is 0 it outputs as 0 but if my score is 1, 2, 3, 4, or 5 it outputs as if I answered only one of the questions correct, anyone know why? also I was wondering if there is any other way I could do the score system like for example use a "while loop" maybe?

Upvotes: 0

Views: 68

Answers (2)

Jonath Sujan
Jonath Sujan

Reputation: 103

Change score =+ 1 to score += 1. += and -= is used for incrementation and decrementation. =+ and =- is used for signed integers.

>>> score = 0
>>> score += 1
>>> score
1
>>> score = -1
>>> score =+ 1
>>> score
1

Why do you need pass in the else? Pass is used when you want to skip further execution in loops. In your code snippet, I don't think pass makes any sense.

You can remove the pass if the If-Else statements are not in a loop. It is not mandatory to have an Else if If is used.

score = 0

if Question_1 in ("Cheetah", "CHEETAH", "cheetah"):
  score =+ 1

if Question_2 in ("Sailfish", "SAILFISH", "sailfish"):
  score =+ 1
  
if Question_3 in ("Peregrine Falcon", "peregrine falcon", "PEREGRINE FALCON", "pf"):
  score =+ 1

if Question_4 in ("A decade", "Decade", "DECADE", "decade"):
  score =+ 1

if Question_5 in ("Centimetre", "Centimetres", "CENTIMETRE", "CM", "cm", "centi", "centimetre"):
  score =+ 1

Upvotes: 2

Pythocrates
Pythocrates

Reputation: 553

The operator you need is +=. =+ is just a = with a signed positive integer.

Upvotes: 9

Related Questions