dimanche 3 novembre 2019

Trying to convert list or get rid of quotes around list numbers

This program is number guessing. It generates 4 numbers and allows the user to take guesses. After each guess it will display number of numbers correct and the place they are in. I get no syntax error from running it however even if all numbers are correct it says none are. I believe this is because the number variable has no quotations and the guess list does. Any help appreciated, thanks!

from random import randint

    number = [(randint(0,9)), (randint(0,9)), (randint(0,9)), (randint(0,9))]



    while True:
        guess_int = (int(input('Guess a 4 digit number: ')))
        guess_str = (str(guess_int))
        guess = (list(guess_str))

        numcorrect = 0

        if guess == number:
            print('Correct!')

        elif number[0] == guess[0]:
            numcorrect = numcorrect + 1
            print('You got the first number correct')
        elif number[1] == guess[1]:
            numcorrect = numcorrect + 1
            print('You got the second number correct')
        elif number[2] == guess[2]:
            numcorrect = numcorrect + 1
            print('You got the third number correct')
        elif number[3] == guess[3]:
            numcorrect = numcorrect + 1
            print('You got the forth number correct')

        if numcorrect != 0:
            print('You got', numcorrect, 'numbers correct')

        if numcorrect == 0:
            print('You got no numbers correct')



Aucun commentaire:

Enregistrer un commentaire