samedi 28 mars 2020

Cow and Bull python program test

Hi Guys, hope you are all doing fine, I just wrote a python program, can you tell me is there anything wrong with it, or is it okay ?


to ensure the random number is non-repeated digits

num_list=[]

while len(num_list) < 4:

rnd = random.randint(0,9)

if rnd not in num_list:

num_list.append(rnd)

continue

random_num = int("".join(map(str,num_list)))

random_num_str = str(random_num)

actual_game_starts_here

guesses = 0

playing = True

def compare_num(guess,random_num_str):

i = 0

cow_bull = [0, 0]

for i in range(len(guess)):

    if random_num_str[i] == guess[i]:

        cow_bull[0]  += 1

    else:

        for j in range(len(random_num_str)):

            if guess[i] == random_num_str[j]:

                cow_bull[1] +=1

return  cow_bull

print("Let's play a game of Cowbull!") #explanation

print("I will generate a number, and you have to guess the numbers one digit at a time.")

print("For every number in the wrong place, you get a bull. For every one in the right place, you get a cow.")

print("The game ends when you get 4 cows!")

print("Type exit at any prompt to exit.")

while playing:

guess = input("Guess your best number: ")

if guess == "exit":

    print(f"I guessed the number {random_num_str}")

    break

cowbull = compare_num(guess,random_num_str)

guesses +=1

print(f"You got {cowbull[0]} cows and {cowbull[1]} bull")

if cowbull[0] == 4:

    playing = False

    print("You won the game lad, after " + str(guesses) + " guesses, idiot")

    print(f"I guessed the number {random_num_str}")

    break

else:

    print("Try again bro!")

Aucun commentaire:

Enregistrer un commentaire