mardi 31 juillet 2018

Python- If statements with random numbers not working

I'm very new to programming and I'm trying to write a program in python 3.6 that generates a random number as an "answer" and then the computer has to guess that answer for x number of questions. To keep track of how many questions the computer guesses right, I created a variable called 'right' and if the computer's answer equals the guess, then add one to that variable. However, it does it everytime even if it's wrong. Sorry if this seems stupid, but thank you for your help

import random
def total(x, t):
        for i in range(t):
                cor = 0
                gue = 0
                n = 0
                right = 0
                def ans():
                         cor = random.randint(1,4)
                         print(cor, 'answer')
                def guess():
                         gue = random.randint(1,4)
                         print(gue, 'guess')
                while n <= x:
                         ans()
                         guess()
                         if cor == gue:
                                 right += 1
                         n += 1 
                print(right, n)




Aucun commentaire:

Enregistrer un commentaire