0

I'm making a simple addition game that generates random numbers to add but when the answer is input it says incorrect even when it is clearly correct. Edit: Thanks for helping. It's fixed now

import random
while True:
    print ("Wellcome!")
    level = input("Choose a level from 1 to 2 or type quit")
    if level == "1":
        a = random.randint (1, 10)
        b = random.randint (1, 10)
        answer = input(str(a) + " + " + str(b) + " = ")
        if answer == a + b:
            print ("Good Job!")
        else:
            print ("Incorrect")
    if level == "2":
        a = random.randint (5, 20)
        b = random.randint (5, 20)
        answer = input(str(a) + " + " + str(b) + " = ")
        if answer == a + b:
            print ("Good Job!")
        else:
            print ("Incorrect")
    if level == "quit":
        break
  • 1
    `answer` will be a string, you need to either convert it to an int or convert the result of `a + b` to a string before you compare – Will Jenkins Aug 03 '21 at 14:02
  • 1
    Let's say for example that the chosen random numbers are `3` and `5`, such that the sum is `8`. Let's say the user types `8` and presses Enter. Are you expecting that the result from `input` will be equal to the sum? Why? (Hint: when you asked the user to choose a level, you had to put `"1"` and `"2"` like that in quotes in order to compare them properly. Right? Why? What happened when you tried comparing the input to `1` and/or `2`? Why does that happen? What happened when you tried reading the documentation for `input`?) – Karl Knechtel Aug 03 '21 at 14:05
  • Apart from it being a crucial step for us to be able to help you, creating a [mre] often helps you see the problem yourself. In this case, replace the inputs with hard-coded values and try to recreate the problem – Tomerikoo Aug 03 '21 at 14:06
  • 1
    @Carcigenicate Did you mean `print(type(answer), type(a + b))`? Doing `print("5", 5)` simply outputs `5 5` – Tomerikoo Aug 03 '21 at 14:10
  • @Tomerikoo Oops, you're right. That was a bad suggestion. – Carcigenicate Aug 03 '21 at 14:16

2 Answers2

3

Input returns a string by default. and '6' is not equal to 6. Convert it to int with the int function.

import random
while True:
    print ("Wellcome!")
    level = input("Choose a level from 1 to 2 or type quit")
    if level == "1":
        a = random.randint (1, 10)
        b = random.randint (1, 10)
        answer = input(str(a) + " + " + str(b) + " = ")
        if int(answer) == a + b:
            print ("Good Job!")
        else:
            print ("Incorrect")
    if level == "2":
        a = random.randint (5, 20)
        b = random.randint (5, 20)
        answer = input(str(a) + " + " + str(b) + " = ")
        if int(answer) == a + b:
            print ("Good Job!")
        else:
            print ("Incorrect")
    if level == "quit":
        break

For what it's worth, you could have done some basic debugging yourself:

  1. Try printing the answer and compare it to what you entered
  2. Try printing the type
  3. Print out the random numbers generated and their answer with the sum function.

Read about it here

Datajack
  • 88
  • 3
  • 16
1

Convert the input "answer" to an int before checking whether the input is correct. Currently, you code compares whether the string variable answer is equal to the integer a + b - which will evidently be False.

Change these rows:

if answer == a + b:

to

if int(answer) == a + b:

and it should work.

yuki
  • 745
  • 4
  • 15