0

I made a function for a program I am making, It's a math game where you have to input an answer, and if the answer is right you win. I thought the game was simple, but for some reason, whenever you input the answer, the program says it's wrong, even if the answer is right.

def gameChoice():
    print("what game do you want to play? A math game")
    game_choice = input(">>")
    if game_choice == 'math game':
        number1 = random.randint(1, 30)
        number2 = random.randint(1, 30)
        answer = (number1 + number2)
        print("%d + %d = %d" %(number1, number2, answer))
        player_answer = input(">> ")
        if player_answer == answer:
            print("congrats, you got it right")

        else:
            print("sorry, try again")
DavidG
  • 24,279
  • 14
  • 89
  • 82
julio.c
  • 9
  • 3
  • 5
    `player_answer` is a string but `answer` is an int. You should probably convert `player_answer` to an integer _then_ compare the two – DavidG May 08 '18 at 19:33
  • 3
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – Fred Larson May 08 '18 at 19:49
  • ... https://stackoverflow.com/q/26447498/2823755 is closer but it is for 2.x `raw_input`. – wwii May 08 '18 at 19:50

2 Answers2

0

The code:

player_answer is a string 

and

answer is an int 

which makes the 2 never equal to each other try putting

player_answer = int(input(">> "))

this makes the input automatically a integer

Raul Cacacho
  • 267
  • 1
  • 4
  • 15
0

Instead of int, you could use literal_eval (from the ast built-in). That will allow either a float or an int in case you want to support floats in the future. You'd also want some exception handling in case the user enters a string or just hits Enter.

Then, you'll want to loop until the user gets it right. One possible fix would be:

import random
from ast import literal_eval

def gameChoice():
    print("what game do you want to play? A math game")
    game_choice = input(">>")
    if game_choice == 'math game':
        number1 = random.randint(1, 30)
        number2 = random.randint(1, 30)
        answer = (number1 + number2)

        while True:
            print("%d + %d = %d" % (number1, number2, answer))
            try:
                player_answer = literal_eval(input(">> "))
            except ValueError:
                print('Please enter a number for the answer')
            except SyntaxError:
                print('Please enter an answer')
            else:
                if player_answer == answer:
                    break
                else:
                    print("sorry, try again")

        print("congrats, you got it right")
Troy Hoffman
  • 181
  • 1
  • 5
  • Seems silly to use `ast` here, it's way more work than just trying to cast to `int` and catch if you can't. I'm also of the belief that if you want your code to handle floats, you should have to refactor your code to handle floats. – Adam Smith May 08 '18 at 19:56
  • It's a little overkill in this situation, but I'm not sure if I'd say it's way more work. It's literally just using "literal_eval" instead of "int" and adding an import. I mainly included it as an alternative to int. – Troy Hoffman May 08 '18 at 23:13
  • Not more work for the programmer, but significantly more work for the system. it's way overkill. – Adam Smith May 08 '18 at 23:14