Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lion and hyena
#1
Problem 1:

Create a program that will play the “lion and hyena” game with the user.

The game works like this:

Computer randomly generates a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “lion”. For every digit the user guessed correctly in the wrong place is a “hyena.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over.

Keep track of the number of guesses the user makes throughout the game and tell the user at the end.

Say the number generated by the computer is 1038. An example interaction could look like this:

Example:

Welcome to the Cows and Bulls Game!
Enter a number:
>>> 1234
2 cows, 0 bulls
>>> 1256
1 cow, 1 bull
...
>>> 1038
Congratulations! You guessed the number in 5 attempts.
Do I have errors in the code? If so, can you fix it?
My code is

import random
 
def num_checker(guess_num,answer):
    guess_num=list(str(guess_num))
    ans=list(str(answer))
    cow=0
    bull=0
    for a in range(0,4):
        if guess_num[a]==ans[a]:
            bull+=1
            ans[a]=10
            guess_num[a]=20
 
    for a in range(0,4):
        for b in range (0,4):
            if guess_num[a]==ans[b]:
                cow+=1
                ans[b]=30
                break
 
    final=[bull,cow]
    return final
 
#..................................................
ans=random.randrange(1000,10000)
print("this is the program to gues a four digit number")
 
while True:
    print("just for reference answer is:",ans)
    num_typed=int(input("please guess a four digit the number?\n "))
    reply=num_checker(num_typed,ans)
    if reply==[4,0]:
        print("correct")
        print(reply[0],"bull",reply[1],"cows and the ans is",ans)
        break
    else:
        print(reply[0],"bulls",reply[1],"cows")
Reply
#2
Is it 'lion and hyena' or 'cows' and 'bulls'?

Aside that if number is '1038' how come '1256' has one 'cow' and one 'bull'? One 'cow' is number '1', but there is no 2, 5 or 6 in 1038
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Hi Azilkhan,
i refactored your code a little bit. ;-)
The code to calculate the bulls and cows can be simplified using list comprehensions and inbuild function sum().
A boolean True is evaluated as 1 and False as 0. And these numbers can then summed together.

In addition you need a function to generate valid random "numbers" as all digits in this game must be different.
A simple random value might also give you "1000" with three digits the same.

And your random.randrange(1000, 10000) does not give you values less than 1000, like "0123"

Here´s the code:
import random
  
def num_checker(guess, answer):
    bulls = sum(guess[i] == answer[i] for i in range(len(answer)))
    cows = sum(guess[i] in answer and guess[i] != answer[i] for i in range(len(answer)))
    return bulls, cows

def get_valid_random(size):
    digits = ""
    while len(digits) < size:
        digit = random.choice("0123456789")
        if not digit in digits:
            digits += digit
    return digits
        
answer = get_valid_random(4)

while True:
    guess = input("Enter number with four different digits: ")
    bulls, cows = num_checker(guess, answer)
    if bulls == 4:
        print("Correct")
        break
    else:
        print(f"You got {bulls} bulls and {cows} cows")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020