Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What are the errors in this python code?

import os

print ("The OCR Controlled Assessment Arithmetic Maths Quiz")
name = input("What is your name? ")
print ("Welcome to our OCR Quiz ", name)

score = 0  
question = 0
while question = 9:
   operator = random.randint(1,3)
   num1 = random.randint(1,12)
   num2 = random.randint(1,12)
   if operator == 1:
      print ("What is {} + {}?".format(num1,num2))
      ans = num1 + num2
   elif operator == 2:
      print ("What is {} * {}?".format(num1,num2))
      ans = num1 * num2
   elif operator == 3:
      print ("What is {} - {}?".format(num1,num2))
      ans = num1 - num2

      while True:
         try:
            user_ans = int(input())
         except ValueError:
               print ("That is not a valid answer!")
               continue
         else:
            break
         if user_ans == ans:
            print("Correct!")
            question += 1
            score += 1
         else:
            print("Nice Try!")
            question += 1
            score += 0

            print ("you scored ()/10".format(score))
            if score == 9:
               print("Excellemt Job!")
            elif score == 6:
               print ("Well Done!")
            elif score == 4:
               print("Try Harder!")

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Rebecca;

Welcome to Treehouse!

The first issue I see is that in your while loop you have while question = 9, line 9. A single equals sign, = assigns value, did you mean to use == which tests for equality?

Also, you have a typo on line 42. I think you probably meant to use Excellent and not Excellemt.

Post back with other questions.

Happy coding,

Ken

import random

import os

print ("The OCR Controlled Assessment Arithmetic Maths Quiz") name = input("What is your name? ") print ("Welcome to our OCR Quiz ", name)

score = 0
question = 0

while question <10: operator = random.randint(1,3) num1 = random.randint(1,12) num2 = random.randint(1,12) if operator == 1: print ("What is {} + {}?".format(num1,num2)) ans = num1 + num2 elif operator == 2: print ("What is {} * {}?".format(num1,num2)) ans = num1 * num2 elif operator == 3: print ("What is {} - {}?".format(num1,num2)) ans = num1 - num2

user_ans = int(input())

if user_ans == ans: print("Correct!") question += 1 score += 1 else: print("Nice Try!") question += 1 score += 0 print ("you scored ()/10")

if score == 9: print("Excellent Job!") elif score == 6: print ("Well Done!") elif score == 4: print("Try Harder!")

this is my new code... how do i get the score to be 10/ 10 etc ?