I am learning python from CS106. Please review my code for Khan Sole Academy, I think my code not efficient. Please add critique and advice for beginner like me.
Generate 2 random numbers and add those numbers. If the user supplied answer is correct, row will increase by 1, goal is 3. If answer is false, row will be reset to zero.
Question From Assignment CS106
Your program should be able to generate simple addition problems that involve adding two 2-digit integers (i.e., the numbers 10 through 99). The user should be asked for an answer to each problem. Your program should determine if the answer was correct or not, and give the user an appropriate message to let them know. Your program should keep giving the user problems until the user has gotten 3 problems correct in a row.
(Note: the number of problems the user needs to get correctly in a row to complete the program is just one example of a good place to specify a constant in your program)
import random MIN_NUM = 10 MAX_NUM = 99 row = 0 def main(): """ Pre Condition: Generate random number. Additional num 1 and num 2 Post Condition : Check user answers. If answer correct add row, if incorrect reset row to zero """ row = 0 while row < 3: # number generator num_1 = random.randint(MIN_NUM, MAX_NUM) num_2 = random.randint(MIN_NUM, MAX_NUM) print("What is " + str(num_1) + " + " + str(num_2) + " ?") total = num_1 + num_2 #user answer user_input = int(input("Your answer: ")) # Check user answer # if answer is incorrect, reset row to zero if user_input == total: row += 1 print("Correct! You've gotten " + str(row) +" correct in a row ") else: print("Incorrect. The expected answer is " + str(total)) row = 0 if __name__ == "__main__": main()