lundi 9 janvier 2023

designing a game in python where random mathematical problems appear for the user to solve

  1. The code is supposed to generate random equations for the user to answer with num1 being in the range of 1 to 9 while num2 in the range 0,10
  2. The user starts with 100 points
  3. if user enters x the program terminates
  4. if user enter wrong answer the score decreases by 10
  5. if user enters correct answer the score increases by 9

I've been successful in doing steps one to 3 however when it comes to checking if the inputted answer by the user is correct it always returns it as wrong even if its correct example figure1

I assume this is because my code doesn't evaluate the question and been trying to figure out why

import random


# set variables
score = 100  # score of user
operator = ["+", "-", "*", "/"] #operators
number_1 = random.randint(1, 9)  # this variable will be a random number between 1 and 9
number_2 = random.randint(0, 10)  # this variable will be a random number between 0 and 10


# this function prints the dashes
def dash():
    print("-" * 50)

while score >= 0 and score <= 200:
    dash()
    print("You currently hold: ", score, " points")
    print("Get more than 200 points you win, under 0 you lose")

    sign = random.choice(operator)  # chooses random operator
    real_answer = number_1,sign,number_2

    print(str(number_1) + str(sign) + str(number_2), "?")
    dash()
    print("Press x to close program")
    answer = input("Enter your guess of number: ")

    if answer == "x":
        print("Program has been terminated properly")
        break
    elif answer == real_answer:
        score = score + 9
        continue
    elif answer  != real_answer:
        score = score - 10
        continue

if score < 0:
        print("Unlucky, You lost")

elif score > 200:
    print("CONGRATULATIONS YOU WON")



Aucun commentaire:

Enregistrer un commentaire