-5
\$\begingroup\$
import random
student_teacher = input("are you a student or teacher")
if student_teacher == "student":
    group = input("what class are you in")
    if group == "1":
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                 correct = x * y #Calculates the correct answer
                 print("What is",x,"x",y,) #Multiplication question asked
                 answer = int(input("Answer")) #Students answer
                 if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                 else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
               correct = x - y #Calculates the correct answer
               print("What is",x,"-",y,) #Subtraction question asked
               answer = int(input("Answer")) #Students answer
               if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
               else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_1 = group, name, score
    if group == "2":
        input ("are you a student")
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                 correct = x * y #Calculates the correct answer
                 print("What is",x,"x",y,) #Multiplication question asked
                 answer = int(input("Answer")) #Students answer
                 if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                 else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
               correct = x - y #Calculates the correct answer
               print("What is",x,"-",y,) #Subtraction question asked
               answer = int(input("Answer")) #Students answer
               if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
               else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_1 = group, name, score
    if group == "3":
        input ("are you a student")
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                correct = x * y #Calculates the correct answer
                print("What is",x,"x",y,) #Multiplication question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
                correct = x - y #Calculates the correct answer
                print("What is",x,"-",y,) #Subtraction question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
                else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_1 = group, name, score

student_teacher = input("are you a student or teacher")
if student_teacher == "student":
    group = input("what class are you in")
    if group == "1":
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                 correct = x * y #Calculates the correct answer
                 print("What is",x,"x",y,) #Multiplication question asked
                 answer = int(input("Answer")) #Students answer
                 if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                 else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
               correct = x - y #Calculates the correct answer
               print("What is",x,"-",y,) #Subtraction question asked
               answer = int(input("Answer")) #Students answer
               if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
               else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_2 = group, name, score
    if group == "2":
        input ("are you a student")
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                 correct = x * y #Calculates the correct answer
                 print("What is",x,"x",y,) #Multiplication question asked
                 answer = int(input("Answer")) #Students answer
                 if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                 else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
               correct = x - y #Calculates the correct answer
               print("What is",x,"-",y,) #Subtraction question asked
               answer = int(input("Answer")) #Students answer
               if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
               else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_2 = group, name, score
    if group == "3":
        input ("are you a student")
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                correct = x * y #Calculates the correct answer
                print("What is",x,"x",y,) #Multiplication question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
                correct = x - y #Calculates the correct answer
                print("What is",x,"-",y,) #Subtraction question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
                else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_2 = group, name, score

student_teacher = input("are you a student or teacher")
if student_teacher == "student":
    group = input("what class are you in")
    if group == "1":
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                 correct = x * y #Calculates the correct answer
                 print("What is",x,"x",y,) #Multiplication question asked
                 answer = int(input("Answer")) #Students answer
                 if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                 else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
               correct = x - y #Calculates the correct answer
               print("What is",x,"-",y,) #Subtraction question asked
               answer = int(input("Answer")) #Students answer
               if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
               else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_3 = group, name, score
    if group == "2":
        input ("are you a student")
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                 correct = x * y #Calculates the correct answer
                 print("What is",x,"x",y,) #Multiplication question asked
                 answer = int(input("Answer")) #Students answer
                 if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                 else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
               correct = x - y #Calculates the correct answer
               print("What is",x,"-",y,) #Subtraction question asked
               answer = int(input("Answer")) #Students answer
               if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
               else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_3 = group, name, score
    if group == "3":
        input ("are you a student")
        name = input("What is your name") #Asks the user to enter their name
        print("Hello",name) #Prints a wlcome message to the user
        score = 0 #Sets the starting score to 0
        times = 0 #Sets the number of answered questions to 0
        while times <= 9: #While loop set up so that 10 questions are asked
              times = times + 1 #Adds 1 to the number of questions asked
              print("#",times) #Prints how many questions have been answered
              x = random.randrange(1,10) #Randomises the first number in the question
              y = random.randrange(1,10) #Randomises the second number in the question
              sign = random.randrange(1,4) #Randomises the sign for the question
              if sign == 1: #If statement for an addition question
                correct = x + y #Calculates the correct answer
                print("What is",x,"+",y,) #Addition question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                    print("That is the correct answer") #response for correct answers
                    score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                  print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 2: #If statement for a subtraction question
                correct = x * y #Calculates the correct answer
                print("What is",x,"x",y,) #Multiplication question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer: #If it is the correct answer
                       print("That is the correct answer") #response for correct answers
                       score = score + 1 #Adds 1 to the score
                else: #If its is an incorrect answer
                       print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
              if sign == 3: #If statement for a multiplication question
                correct = x - y #Calculates the correct answer
                print("What is",x,"-",y,) #Subtraction question asked
                answer = int(input("Answer")) #Students answer
                if correct == answer:
                  print("That is the correct answer")
                  score = score + 1
                else:
                  print("That is incorrect, the correct answer is",correct)
        print (name,"your total score is",score)
        student_3 = group, name, score
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question. \$\endgroup\$ – SuperBiasedMan Oct 27 '15 at 11:41
  • 1
    \$\begingroup\$ Also, as we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. For examples of good titles, check out Best of Code Review 2014 - Best Question Title Category You may also want to read How to get the best value out of Code Review - Asking Questions. \$\endgroup\$ – SuperBiasedMan Oct 27 '15 at 11:42
5
\$\begingroup\$

Repetition

I'd say half, if not more of your code is repeated.

Make use of functions to avoid repetition!

You're doing the same thing 3 times, the only difference being the student_* variable:

  • Why do you need 3 separate variables? Just one the code 3 times, or put a loop in to run 3 times.
  • Use functions!
def quiz(group):
    name = input("What is your name") #Asks the user to enter their name
    print("Hello",name) #Prints a wlcome message to the user
    score = 0 #Sets the starting score to 0
    times = 0 #Sets the number of answered questions to 0
    while times <= 9: #While loop set up so that 10 questions are asked
          times = times + 1 #Adds 1 to the number of questions asked
          print("#",times) #Prints how many questions have been answered
          x = random.randrange(1,10) #Randomises the first number in the question
          y = random.randrange(1,10) #Randomises the second number in the question
          sign = random.randrange(1,4) #Randomises the sign for the question
          if sign == 1: #If statement for an addition question
            correct = x + y #Calculates the correct answer
            print("What is",x,"+",y,) #Addition question asked
            answer = int(input("Answer")) #Students answer
            if correct == answer: #If it is the correct answer
                print("That is the correct answer") #response for correct answers
                score = score + 1 #Adds 1 to the score
            else: #If its is an incorrect answer
              print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
          if sign == 2: #If statement for a subtraction question
             correct = x * y #Calculates the correct answer
             print("What is",x,"x",y,) #Multiplication question asked
             answer = int(input("Answer")) #Students answer
             if correct == answer: #If it is the correct answer
                   print("That is the correct answer") #response for correct answers
                   score = score + 1 #Adds 1 to the score
             else: #If its is an incorrect answer
                   print("That is incorrect, the correct answer is",correct) #Response to an incorrect answer
          if sign == 3: #If statement for a multiplication question
           correct = x - y #Calculates the correct answer
           print("What is",x,"-",y,) #Subtraction question asked
           answer = int(input("Answer")) #Students answer
           if correct == answer:
              print("That is the correct answer")
              score = score + 1
           else:
              print("That is incorrect, the correct answer is",correct)
    print (name,"your total score is",score)
    return (group, name, score)

That just cut down on 2 thirds of your code!


Comments

Comments are good, but too many gets tedious. Only use comments when the code is doing something that isn't self explanatory.

Example:

  • Asks the user to enter their name
  • Prints a wlcome message to the user
  • Adds 1 to the number of questions asked
  • If it is the correct answer

etc... All of these are obvious! They are unlikely to help anyone -- even yourself! So get rid of them


Inputs

name = input("What is your name")

Leave a space after that last letter so when the program is running, you're not typing right next to the question. And add a question mark! :)

if group == "1":

If you're looking for an integer, look for an integer. So when you make the group variable, try to make it an integer:

try:
    group = int(input("What class are you in? ")) #with a space and question mark 
except ValueError:
    print("Please enter a NUMBER")

There you are adding error messages as well!


Other things

When adding to the score/times, simply do:

score += 1
times += 1

That increments the variable by 1


All of that in account your code is much cleaner and shorter!:

import random

def quiz(group):
    name = input("What is your name")
    print("Hello",name) 
    score = 0 #Sets the starting score to 0
    times = 0 #Sets the number of answered questions to 0
    while times <= 9: #While loop set up so that 10 questions are asked
          times += 1
          print("#",times) #Prints how many questions have been answered
          x = random.randrange(1,10) #Randomises the first number in the question
          y = random.randrange(1,10) #Randomises the second number in the question
          sign = random.randrange(1,4) #Randomises the sign for the question
          if sign == 1: #If statement for an addition question
            correct = x + y 
            print("What is",x,"+",y,)
            answer = int(input("Answer"))
            if correct == answer:
                print("That is the correct answer")
                score += 1 
            else: 
              print("That is incorrect, the correct answer is",correct) 
          if sign == 2: #If statement for a subtraction question
             correct = x * y 
             print("What is",x,"x",y,) 
             answer = int(input("Answer")) 
             if correct == answer: 
                   print("That is the correct answer") 
                   score += 1
             else: 
                   print("That is incorrect, the correct answer is",correct)
          if sign == 3: #If statement for a multiplication question
           correct = x - y
           print("What is",x,"-",y,) 
           answer = int(input("Answer")
           if correct == answer:
              print("That is the correct answer")
              score += 1
           else:
              print("That is incorrect, the correct answer is",correct)
    print (name,"your total score is",score)
    return (group, name, score)

for i in range(3): #Run the code 3 times
    student_teacher = input("are you a student or teacher")
    if student_teacher == "student":
        try:
            group = int(input("What class are you in? ")) #with a space and question mark 
        except ValueError:
            print("Please enter a NUMBER!")
        if group == 1:
            student_1 = quiz(group)        
        if group == 2:
            student_2 = quiz(group)
        if group == 3:
            student_3 = quiz(group)
\$\endgroup\$
  • 2
    \$\begingroup\$ You can still improve by creating a function containing code related to the repetition involved with if sign == X:. Something along the lines of def ask_question(term1, term2, answer, symbol): .... \$\endgroup\$ – Mathias Ettinger Oct 27 '15 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.