Python 3.8 version
I want to shorten my code but i could not find a way to short '#Player 1 and player 2 function'. My knowledge of python is limited with only this functions (if, elif, else, while and for). Please post any solution to my question. It would be better to me learning new tricks and functions.
I have tried this but it didn't work. I can't understand why this code didn't work.
if a1g == a2 or b2 or c2:
Cows1.add("a")
Here is my code for Basic bulls and cows game
from time import sleep
from random import shuffle
print(" ----------------------------------")
print(" - BULLS AND COWS -")
print(" ----------------------------------\n\n")
#sets
Cows1 = set()
Cows2 = set()
Bulls1 = set()
Bulls2 = set()
#creating numbers without dublicates
number1 = list(range(1,10))
number2 = list(range(1,10))
shuffle(number1)
shuffle(number2)
#player 1
a1 = number1[0]
b1 = number1[1]
c1 = number1[2]
d1 = number1[3]
number_player1 = [a1,b1,c1,d1]
#player 2
a2 = number2[0]
b2 = number2[1]
c2 = number2[2]
d2 = number2[3]
number_player2 = [a2,b2,c2,d2]
sleep(1)
ask_user = input("Do you want to see created numbers ? (yes/no): ")
if ask_user == 'yes':
sleep(1)
print("Player 1's numbers:")
print(number_player1)
print("Players 2's numbers:")
print(number_player2)
if ask_user == 'Yes':
sleep(1)
print("Player 1's numbers:")
print(number_player1)
print("Players 2's numbers:")
print(number_player2)
#main loop
while True:
#player 1 guesses
sleep(1)
print("\nPlayer 1 guess:\n")
sleep(1)
a1g = int(input("Number 1: "))
b1g = int(input("Number 2: "))
c1g = int(input("Number 3: "))
d1g = int(input("Number 4: "))
#player 1 function
if a1g == a2:
Bulls1.add("a")
elif a1g is b2 :
Cows1.add("a")
elif a1g is c2:
Cows1.add("a")
elif a1g is d2:
Cows1.add("a")
if b1g == b2:
Bulls1.add("b")
elif b1g is a2:
Cows1.add("b")
elif b1g is c2:
Cows1.add("b")
elif b1g is d2:
Cows1.add("b")
if c1g == c2:
Bulls1.add("c")
elif c1g is b2:
Cows1.add("c")
elif c1g is a2:
Cows1.add("c")
elif c1g is d2:
Cows1.add("c")
if d1g == d2:
Bulls1.add("d")
elif d1g is b2:
Cows1.add("d")
elif d1g is c2:
Cows1.add("d")
elif d1g is a2:
Cows1.add("d")
if len(Bulls1) == 4:
print("\nPLAYER 1 WON")
sleep(1)
print("Player 1 numbers:\n")
print(number_player1)
sleep(1)
print("Player 2 numbers:\n")
print(number_player2)
sleep(1)
exit(input("Press enter to exit"))
#player 1 bull and cow
print(f"{len(Bulls1)} Bull")
print(f"{len(Cows1)} Cow")
#player 2 guesses
print("\nPlayer 2 guess:\n")
sleep(1)
a2g = int(input("Number 1: "))
b2g = int(input("Number 2: "))
c2g = int(input("Number 3: "))
d2g = int(input("Number 4: "))
#player 2 function
if a2g == a1:
Bulls2.add("a")
elif a2g == b1 :
Cows2.add("a")
elif a2g == c1:
Cows2.add("a")
elif a2g == a1:
Cows2.add("a")
if b2g == b1:
Bulls2.add("b")
elif b2g == a1:
Cows2.add("b")
elif b2g == c1:
Cows2.add("b")
elif b2g == d1:
Cows2.add("b")
if c2g == c1:
Bulls2.add("c")
elif c2g == b1:
Cows2.add("c")
elif c2g == a1:
Cows2.add("c")
elif c2g == d1:
Cows2.add("c")
if d2g == d1:
Bulls2.add("d")
elif d2g == b1:
Cows2.add("d")
elif d2g == c1:
Cows2.add("d")
elif d2g == a1:
Cows2.add("d")
if len(Bulls2) == 4:
print("\nPLAYER 2 WON")
sleep(1)
print("Player 1 numbers:\n")
print(number_player1)
sleep(1)
print("Player 2 numbers:\n")
print(number_player2)
sleep(1)
exit(input("Press enter to exit"))
#player 1 bull and cow
print(f"{len(Bulls2)} Bull")
print(f"{len(Cows2)} Cow")
Cows1.clear()
Cows2.clear()
Bulls1.clear()
Bulls2.clear()