Nov-21-2019, 07:13 AM
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
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")