# Arithmetic quiz for student groups

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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
student_3 = group, name, score
• 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. Oct 27, 2015 at 11:41
• 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. Oct 27, 2015 at 11:42

# 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
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
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
score = score + 1
else:
print("That is incorrect, the correct answer is",correct)
return (group, name, score)

## That just cut down on 2 thirds of your code!

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
• 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:

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,)
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,)
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,)
score += 1
else:
print("That is incorrect, the correct answer is",correct)
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: