How to make my code more efficient?

+1 Steven the awesome · September 10, 2014

class Quiz:
    # aantal punten gezet naar nul en een array/list met antwoorden
    point = 0
    antwoorden = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

    def easy_questions(self):
        while True:
            try:
                vraag1 = int(input("What is 10 x 10 = "))
                if vraag1 != 100:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[0]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[0]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag2 = int(input("What is 10 x 9 = "))
                if vraag2 != 90:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[1]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[1]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag3 = int(input("What is 10 x 8 = "))
                if vraag3 != 80:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[2]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[2]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag4 = int(input("What is 10 x 7 = "))
                if vraag4 != 70:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[3]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[3]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag5 = int(input("What is 10 x 6 = "))
                if vraag5 != 60:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[4]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[4]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag6 = int(input("What is 10 x 5 = "))
                if vraag6 != 50:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[5]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[5]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag7 = int(input("What is 10 x 4 = "))
                if vraag7 != 40:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[6]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[6]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag8 = int(input("What is 10 x 3 = "))
                if vraag8 != 30:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[7]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[7]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag9 = int(input("What is 10 x 2 = "))
                if vraag9 != 20:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[8]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[8]))
                    self.point += 1
                    print("Your note until now is: " + str(self.point))
                vraag10 = int(input("What is 10 x 1 = "))
                if vraag10 != 10:
                    print("The answer is wrong! the correct answer is " + str(self.antwoorden[9]))
                else:
                    print("The answer is correct it was indeed " + str(self.antwoorden[9]))
                    self.point += 1
                    print("Your end score is a: " + str(self.point))
                    break
            except ValueError:
                print("Make sure to enter valid number")
            except ZeroDivisionError:
                print("Don't divide zero")


# ask for the class
quiz = Quiz()

#ask for the method
quiz.easy_questions()



Thanks in advance!

Post a Reply

Replies

Oldest  Newest  Rating
+1 Gary Murphy · September 11, 2014
If you are using those exact questions you could use iteration. 
class Quiz:
    point = 0
    def easy_questions(self):
        while True:
            try:
                for question in range(10, 0, -1):
                    vraag = int(input("What is 10 x " + str(question) + " = "))
                    antwoorden = 10 * question
                    if vraag != antwoorden:
                        print("The answer is wrong! the correct answer is " + str(antwoorden))
                    else:
                        print("The answer is correct it was indeed " + str(antwoorden))
                        self.point += 1
                        print("Your note until now is: " + str(self.point))
                break
            except ValueError:
                print("Make sure to enter valid number")
            except ZeroDivisionError:
                print("Don't divide zero")

# ask for the class
quiz = Quiz()

#ask for the method
quiz.easy_questions()
+1 Steven the awesome · September 11, 2014
Thanks!
0 Mel Addison · December 7, 2014
what is iteration?
0 Doug Fresh · December 7, 2014
Iteration is the repetition of a similar step in programming. By recognizing patterns in programs, we can effectively reduce 90 lines of code, down to 26. This makes the code legible and makes it easier to maintain.   
  • 1

Python

107,270 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator