Mini Project - Game - Cows and Bulls #17
Replies: 10 comments
-
import random
num = random.randint(1000, 9999)
num = str(num)
print(num)
play = True
while play:
cows = 0
bulls = 0
geuss = input("Guess a number\n")
if geuss == num:
print("Congrats you got it right")
cows = 4
play = False
else:
for i in range(len(num)):
if num[i] == geuss[i]:
cows+=1
for k in range(len(num)):
if num[i] == geuss[k] and i != k:
bulls +=1
print(f"{cows} cows, {bulls} bulls")
``` |
Beta Was this translation helpful? Give feedback.
-
import random
randNum = str(random.randint(1000, 9999))
while True:
cows = 0
bulls = 0
print(randNum)
x = input("Enter a number of 4 digits (input 'exit' to finish): ")
for i in range(0,4):
if(x == randNum): # Full match
print("Full match")
cows = cows + 4
break
for j in range(0,4):
if(x[i] == randNum[j] and i != j): # Bull cases
#print("Bull cases - Good value wrong position: ",x[i], " y ", randNum[j])
bulls = bulls + 1
elif (x[i] == randNum[j] and i == j): # Cow cases
#print("Cow cases - Good value good position: ",x[i], " y ", randNum[j] )
cows = cows + 1
else:
pass
print("Score is\nCows: ",cows, " and Bulls: ", bulls) |
Beta Was this translation helpful? Give feedback.
-
from random import randint
print("Welcome to the 'Cow and Bull' game!")
random_number = randint(1000, 9999)
random_number = str(random_number)
guesses = 0
while True:
print(random_number)
user_number = input("Enter a number from 1000 to 9999: ")
guesses += 1
cow = 0
bull = 0
for i in range(4):
# Check for COW
if random_number[i] == user_number[i]:
cow += 1
# Check for BULL
for j in range(4):
if random_number[i] == user_number[j] and i != j:
bull += 1
if cow == 4:
break
print(f"Cows: {cow}")
print(f"Bulls: {bull}")
print("You have won the 'Cow and Bull' game!")
print(f"Times guessed: {guesses}") |
Beta Was this translation helpful? Give feedback.
-
from random import randint, seed
def cows(num, inp):
cont = 0
for i in range(inp.__len__()):
if num[i] == inp[i]:
cont+=1
return cont
def bulls(num, inp):
cont = 0
for i in range(inp.__len__()):
if inp[i] in num:
cont+=1
return cont
seed(1)
n = randint(1000, 9999)
while(True):
inp = int(input("Make a guess"))
if(inp == n):
print(f"Yes the number it's {inp}")
else:
cow = cows(str(n), str(inp))
bull = bulls(str(n), str(inp))
print(f"You have: {cow} cows and {bull-cow} bulls") |
Beta Was this translation helpful? Give feedback.
-
import random
def getDigits(num):
return [int(i) for i in str(num)]
def noDuplicates(num):
num_li = getDigits(num)
if len(num_li) == len(set(num_li)):
return True
else:
return False
def generateNum():
while True:
num = random.randint(1000, 9999)
if noDuplicates(num):
return num
def numOfBullsCows(num, guess):
bull_cow = [0, 0]
num_li = getDigits(num)
guess_li = getDigits(guess)
for i, j in zip(num_li, guess_li):
if j in num_li:
if j == i:
bull_cow[0] += 1
else:
bull_cow[1] += 1
return bull_cow
def main():
num = generateNum()
tries = 0
while True:
guess = int(input("Enter your guess: "))
if not noDuplicates(guess):
print("Number should have no duplicates, Try again.")
continue
if guess < 1000 or guess > 9999:
print("Enter a 4 digit number only. Try again.")
continue
bull_cow = numOfBullsCows(num, guess)
print(f"{bull_cow[0]} bulls, {bull_cow[1]} cows")
tries += 1
if bull_cow[0] == 4:
print("You guessed right! It took", tries, "tries.")
break
if __name__ == '__main__':
main() |
Beta Was this translation helpful? Give feedback.
-
import random
def getDigits(num):
return [int(i) for i in str(num)]
def noDuplicates(num):
num_li = getDigits(num)
if len(num_li) == len(set(num_li)):
return True
else:
return False
def generateNum():
while True:
num = random.randint(1000, 9999)
if noDuplicates(num):
return num
def numOfBullsCows(num, guess):
bull_cow = [0, 0]
num_li = getDigits(num)
guess_li = getDigits(guess)
for i, j in zip(num_li, guess_li):
if j in num_li:
if j == i:
bull_cow[0] += 1
else:
bull_cow[1] += 1
return bull_cow
def main():
num = generateNum()
tries = 0
while True:
guess = int(input("Enter your guess: "))
if not noDuplicates(guess):
print("Number should have no duplicates, Try again.")
continue
if guess < 1000 or guess > 9999:
print("Enter a 4 digit number only. Try again.")
continue
bull_cow = numOfBullsCows(num, guess)
print(f"{bull_cow[0]} bulls, {bull_cow[1]} cows")
tries += 1
if bull_cow[0] == 4:
print("You guessed right! It took", tries, "tries.")
break
if __name__ == '__main__': |
Beta Was this translation helpful? Give feedback.
-
from random import randint
print("Welcome to the 'Cow and Bull' game!")
random_number = randint(1000, 9999)
random_number = str(random_number)
guesses = 0
while True:
print(random_number)
user_number = input("Enter a number from 1000 to 9999: ")
guesses += 1
cow = 0
bull = 0
for i in range(4):
# Check for COW
if random_number[i] == user_number[i]:
cow += 1
# Check for BULL
for j in range(4):
if random_number[i] == user_number[j] and i != j:
bull += 1
if cow == 4:
break
print(f"Cows: {cow}")
print(f"Bulls: {bull}")
print("You have won the 'Cow and Bull' game!")
print(f"Times guessed: {guesses}" |
Beta Was this translation helpful? Give feedback.
-
Cows and bulls guessing gameUsing list comprehension and some input manipulation # cows and bulls game
import random
def main():
# Generate random 4 digit number
num = random.randint(1000,9999)
print(F"Welcome to the Cows and Bulls game!")
print("-- Instructions: Guess a 4-digit number. --\nCow indicate correct number in correct digit.\nBull indicates correct number but wrong digit.")
# print(num)
# keep track of tries
tries = 0
# Start Game
guess = 0
while guess != num:
guess = int(input("Enter a (4-digit) number: "))
try:
# count bulls
bulls = sum([str(num).count(str(guess)[i]) for i in range(4) if str(guess)[i] in list(set(str(num)))])
# check cows
cows = sum([1 if str(num)[i] == str(guess)[i] else 0 for i in range(4)])
# print results
print(F"cows: {cows} \t, \tbulls: {bulls-cows}")
# tries +1
tries += 1
except:
print("Wrong input, guess again...")
# win and total tries
print(f"You correctly guessed: {num} \nNumber of tries: {tries}")
# run main
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
-
import random
number = random.randint(1000,9999)
print(number)
user = str(input("Enter number"))
cow=0
bull= 0
i=0
while i < len(user):
if user[i] == str(number)[i]:
print(user[i])
cow += 1
bull += 1
i=i+1
print("cow",cow, "bull",bull) |
Beta Was this translation helpful? Give feedback.
-
1.- from random import randrange
def cowsbulls(num, number):
cow = 0
bulls = 0
correctnum = []
for i in range(len(str(num))):
if str(num)[i] == str(number)[i]:
cow = cow + 1
correctnum.append(i)
for i in range(len(str(num))):
if i not in correctnum:
for y in range(len(str(num))):
if y not in correctnum:
if str(num)[i] == str(number)[y]:
bulls = bulls + 1
print("Cows: ", cow, " Bulls: ", bulls)
def main():
number = randrange(1000,9999)
while True:
print(" Welcome to the Cows and Bulls Game! ")
num = int(input("Enter a number: "))
if number == num:
print("Correct number")
break
else:
cowsbulls(num, number)
main() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Create a program that will play the “cows and bulls” game with the user. The game works like this:
Randomly generate 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 “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” 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:
Until the user guesses the number.
Beta Was this translation helpful? Give feedback.
All reactions