How to I write these steps in Python?

Update:

The game ends when the user wins by having 4 bulls or enters X to Forfeit when asked to enter a guess

Only 1 game is played with as many turns as needed to either "Win" or "Forfeit"

After the game has been played, print the game statistics of number of turns and/or forfeit.

1 Answer

Relevance
  • 6 months ago

    Hi, AlexandraMia.

    You can try something like this:

    def calcCowsAndBulls(guess):

    ... if (guess in "x" "X"):

    ... ... return -1, -1

    ... cows, bulls = 0, 0

    ... # your code here to calculate cows and bulls

    ... return cows, bulls

    def main():

    ... turns = 0

    ... while (True):

    ... ... guess = input ("Enter guess: ")

    ... ... cows, bulls = calcCowsAndBulls (guess)

    ... ... if (guess in "x" "X"):

    ... ... ... break

    ... ... turns += 1

    ... ... if (bulls == 4):

    ... ... ... print ("You win!")

    ... ... ... break

    ... ... else:

    ... ... ... print ("Cows:", cows, "and Bulls:", bulls)

    ... # while

    ... if (guess in "x" "X"):

    ... ... print ("The game is forfeit!")

    ... else:

    ... ... print ("Number of turns =", turns)

    #end main

    main()

    #Don't include the dots ("... ") - they're just to show indentation. Use a tab or spaces in your code.

    • Login to reply the answers
Still have questions? Get your answers by asking now.