Skip to content Skip to sidebar Skip to footer

Python Multiple Number Guessing Game

I am trying to create a number guessing game with multiple numbers. The computer generates 4 random numbers between 1 and 9 and then the user has 10 chances to guess the correct nu

Solution 1:

You need four guesses to match for random numbers, you can also shorted your code using a list comp:

from random import randint

guessesTaken = 0
randomNumber = []

Guess = []
for x inrange(4):
        tempNumber = str(randint(1, 9)) # compare string to string 
        randomNumber.append(tempNumber)
        Guess.append(input("Guess Number: "))

print("".join(["Y"if a==b else"N"for a,b inzip(Guess,randomNumber)]))

You can also use enumerate to check elements at matching indexes:

print("".join(["Y"if randomNumber[ind]==ele else"N"for ind, ele in enumerate(Guess)]))

To give the user guesses in a loop:

from random import randint

guessesTaken = 0
randomNumber = [str(randint(1, 9))  for _ inrange(4)] # create list of random numswhile guessesTaken < 10: 
    guesses = list(raw_input("Guess Number: ")) # create list of four digits
    check = "".join(["Y"if a==b else"N"for a,b inzip(guesses,randomNumber)])
    if check == "YYYY": # if check has four Y's we have a correct guessprint("Congratulations, you are correct")
        breakelse:
        guessesTaken += 1# else increment guess count and ask againprint(check)

Solution 2:

Right now you're only asking the user for one guess, and appending the guess to the Guess list. So the Guess list has one element, but you're using Guess[1], Guess[2], etc., which of course results in the IndexError

Solution 3:

I'll rearrange your code a bit, so it doesn't stray too far from what you've done.

from random import randint

guessesTaken = 0
randomNumbers = []
Guess = [] # Combine your guesses with your loopfor x inrange(4):
    tempNumber = randint(1, 9)
    randomNumbers.append(tempNumber)
    # This should be done four times too# In Python 2, instead of this:# Guess.append(input("Guess Number: ")) # do this:
    Guess.append(int(raw_input("Guess Number: "))) #  raw_input and pass to int # in python 3, raw_input becomes input, so do this instead:# Guess.append(int(input("Guess Number: "))) print(randomNumbers)
print(Guess)

You can combine these in a loop to avoid the repetitive code:

if randomNumbers[0] ==Guess[0]:
    print("Y")
else:
    print("N")
if randomNumbers[1] ==Guess[1]:
    print("Y")
else:
    print("N")
if randomNumbers[2] ==Guess[2]:
    print("Y")
else:
    print("N")
if randomNumbers[3] ==Guess[3]:
    print("Y")
else:
    print("N")

Perhaps, to print your desired result e.g. YNNY, like this:

result = []
for index in range(4):
    if randomNumbers[index] == Guess[index]:
        result.append("Y")
    else:
        result.append("N")
print(''.join(result))

If you want terser code use Python's ternary operation:

result = []
forindex in range(4):
    result.append("Y"if randomNumbers[index] == Guess[index] else"N")
print(''.join(result))

Or use the fact that True == 1 and False == 0 as indexes:

result = []
forindex in range(4):
    result.append("NY"[randomNumbers[index] == Guess[index]])
print(''.join(result))

Post a Comment for "Python Multiple Number Guessing Game"