How to count number of tries in python

So I'm trying to coutn how many tries it takes to guess the mixed up words. Quite frankly, I've been stuck for so long because this code doesn't work at all. Please help.

#Word Jumble Game
import random
import string


words = ['Jumble', 'Star', 'Candy', 'Wings', 'Power', 'String', 'Shopping', 'Blonde', 'Steak', 'Speakers', 'Case', 'Stubborn', 'Cat', 'Marker', 'Elevator', 'Taxi', 'Eight', 'Tomato', 'Penguin', 'Custard']

def jumbled():
    word = string.lower(random.choice(words))
    jumble = list(word)
    random.shuffle(jumble)
    scrambled = "".join(jumble)
    print '\n',scrambled,'\n'

    guess = raw_input('Guess the word: ')

    count=0

    if guess == word:
        print '\n','Correct!'
    else:
        print '\n','Try again!','\n',jumbled()
    count+=1

jumbled()

How to count number of tries in python

Img Source : funbrain.com

I found that most of the projects one can find online of making a Number Guessing Game in Python are quite cumbersome and use a ton of functions.(You can play the number guessing game online here).

In this article i have implemented the logic of the same game by using a single function which is convenient to understand and at the same time easy to code.

Objectives of the game-:

  1. Include an ASCII art logo.
  2. User submits his guess for a number between 1 and 100.
  3. Check user’s guess against actual answer. Print “Too high.” or “Too low.” depending on the user’s answer.
  4. If they got the answer correct, show the actual answer to the player.
  5. Track the number of turns remaining.
  6. If they run out of turns, provide feedback to the player.
  7. Include two different difficulty levels (e.g., 10 guesses in easy mode, only 5 guesses in hard mode).

Let’s jump right into it!

Step 1: Including an ASCII art logo

Text to ASCII generator can be found here. Just type in the text to be converted and the tool generates an ASCII art form of the text.

Step 2: Inputting settings from the user

import random
MY_GUESS = random.randint(1,100)
print(logo)
print("Welcome to the Number Guessing Game!\nI'm thinking of a number between 1 and 100.\n")
difficulty = str(input("Choose a difficulty. Type 'easy' or 'hard': ")).lower()

We import the random library for the MY_GUESS variable which takes a random integer value from 1 to 100 using randint method of random.

difficulty = str(input("Choose a difficulty. Type 'easy' or 'hard': ")).lower()

The variable difficulty is a string variable which lets the user decide the settings of the game if they want to play it on easy or hard setting.

Step 3: Defining the count function

def count(counts, attempts):
print(f"The correct answer is {MY_GUESS}")
no_of_guesses = False
count = 1
print("You have 10 attempts remaining to guess the number.\n")
while no_of_guesses == False and count <= counts:
guess = int(input("Make a guess: "))
if guess == MY_GUESS:
print(f"You got it! The answer was {MY_GUESS}")
no_of_guesses = True
elif guess < MY_GUESS:
print(f"Too Low.\nGuess Again.\n")
count += 1
attempts -= 1
print(f"You have {attempts} attempts remaining")
elif guess > MY_GUESS:
print(f"Too High.\nGuess Again.\n")
count += 1
attempts -= 1
print(f"You have {attempts} attempts remaining")

The count function takes two arguments “counts” and “attempts”. The count variable is different from the ‘counts’ variable as it is incremented whenever the guess is wrong and the ‘attempts’ variable is decremented after every attempt.

Defining a boolean variable ‘no_of_guesses’:

no_of_guesses = False

The no_of_guesses keeps track of the while loop and when it should be stopped.

Condition for the while loop :

while no_of_guesses == False and count <= counts:
guess = int(input("Make a guess: "))
if guess == MY_GUESS:
print(f"You got it! The answer was {MY_GUESS}")
no_of_guesses = True

If the number of guesses is False and count is less than the counts(argument) we ask the user for his guess. We check for equality with the MY_GUESS variable which is the initial guess taken randomly.

elif guess < MY_GUESS:
print(f"Too Low.\nGuess Again.\n")
count += 1
attempts -= 1
print(f"You have {attempts} attempts remaining")
elif guess > MY_GUESS:
print(f"Too High.\nGuess Again.\n")
count += 1
attempts -= 1
print(f"You have {attempts} attempts remaining")

If however the user’s guess is wrong we increment the count variable by 1 and decrement the attempts by 1. Here, if the guess value is small compared to the MY_GUESS value. We output a hint that the value is “Too Low. Guess Again.” Also, cautioning the user about the number of attempts he has reamining.

Similarly, if the guess value is high compared to the MY_GUESS value. We output a hint that the value is “Too High. Guess Again.” Again, cautioning the user about the number of attempts he has reamining.

Step 4: Calling the ‘count’ function.

if difficulty == 'easy':
count(10, 10)
elif difficulty == 'hard':
count(5, 5)

We pass arguments 10, 10 : counts, attempts for easy mode and 5,5 for hard mode as laid out in the objectives.

print(MY_GUESS) — → Psst, you can use this to check for the number guessed by the computer

I hope this one upholds the old adage by Martin,

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler

Did you like my efforts? If Yes, please follow me to get my latest posts and updates or better still, buy me a coffee!☕

How to count number of tries in python