Skip to content

Instantly share code, notes, and snippets.

@Raj39120
Created May 4, 2020 02:01
Show Gist options
  • Save Raj39120/58b79064ca90b123118904b9ea4e74b2 to your computer and use it in GitHub Desktop.
Save Raj39120/58b79064ca90b123118904b9ea4e74b2 to your computer and use it in GitHub Desktop.
from random import randrange
from time import sleep
def cows_and_bulls_game_2():
a = randrange(1, 9)
b = randrange(1, 9)
c = randrange(1, 9)
d = randrange(1, 9)
print("This program runs the cows ands bulls game where a four-digit number is generated every time you play.")
sleep(5)
print("After the program generates a random four-digit number you have to guess what that number is.")
sleep(4.8)
print("For every correct digit you guess in the correct place, you get a cow, if you guess wrong, you get a bull.")
sleep(5.2)
print("At the end of the game the program will print how many cows and bulls you have.")
sleep(4)
string = str(a) + "" + str(b) + "" + str(c) + "" + str(d) + ""
user_guess = raw_input("Enter your guess over here: ")
count_cows = 0
count_bulls = 0
if str(user_guess)[0] == str(a):
count_cows = count_cows+1
if str(user_guess)[0] != str(a):
count_bulls = count_bulls+1
if str(user_guess)[1] == str(b):
count_cows = count_cows+1
if str(user_guess)[1] != str(b):
count_bulls = count_bulls+1
if str(user_guess)[2] == str(c):
count_cows = count_cows+1
if str(user_guess)[2] != str(c):
count_bulls = count_bulls+1
if str(user_guess)[3] == str(d):
count_cows = count_cows+1
if str(user_guess)[3] != str(d):
count_bulls = count_bulls+1
print("The number is: " + str(string))
print(str(count_cows) + " cows" + ", " + str(count_bulls) + " bulls")
choice = raw_input("If you want to play again type continue over here, if not type exit: ")
if choice == 'continue':
def cows_and_bulls_game():
a_2 = randrange(1, 9)
b_2 = randrange(1, 9)
c_2 = randrange(1, 9)
d_2 = randrange(1, 9)
string_2 = str(a_2) + "" + str(b_2) + "" + str(c_2) + "" + str(d_2) + ""
user_guess_2 = raw_input("Enter your guess over here: ")
count_cows_2 = 0
count_bulls_2 = 0
if str(user_guess_2)[0] == str(a_2):
count_cows_2 = count_cows_2 + 1
if str(user_guess_2)[0] != str(a_2):
count_bulls_2 = count_bulls_2 + 1
if str(user_guess_2)[1] == str(b_2):
count_cows_2 = count_cows_2 + 1
if str(user_guess_2)[1] != str(b_2):
count_bulls_2 = count_bulls_2 + 1
if str(user_guess_2)[2] == str(c_2):
count_cows_2 = count_cows_2 + 1
if str(user_guess_2)[2] != str(c_2):
count_bulls_2 = count_bulls_2 + 1
if str(user_guess_2)[3] == str(d_2):
count_cows_2 = count_cows_2 + 1
if str(user_guess_2)[3] != str(d_2):
count_bulls_2 = count_bulls_2 + 1
print("The number is: " + str(string_2))
print(str(count_cows_2) + " cows" + ", " + str(count_bulls_2) + " bulls")
choice_2 = raw_input("If you want to play again type continue over here, if not type exit: ")
if choice_2 == 'continue':
cows_and_bulls_game()
if choice_2 == 'exit':
return
else:
print("You have not entered a valid choice so we will end the game.")
return
cows_and_bulls_game()
if choice == 'exit':
return
else:
print("You have not entered a valid choice so we will end the game.")
return
cows_and_bulls_game_2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment