Guessing Game: computer generated number between 1 and 100 guessed by user The required output is stated just before the main() function. Is there a better way to achieve it? I feel like I am repeating code unnecessarily but am unable to find a better way to get the result. Not squeezing the code too much, like one liners, but reasonably using better logic.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#Time to play a guessing game.

#Enter a number between 1 and 100: 62
#Too high. Try again: 32
#Too low. Try again: 51
#Too low. Try again: 56

#Congratulations! You got it in 4 guesses.

import random

def main():

    return 0

if __name__ == '__main__':
    main()

print("Time to play a guessing game.")

val = random.randint(1, 100)
guess = 0
count = 1

guess = input("\n\t\tEnter a number between 1 and 100: ")
guess = int(guess)

print("")
print("randint is", val)
print("")

if (guess == val):
    print("\nCongratulations! You got it in", count, "guesses.")

while(guess != val):

    if guess &gt; val:
        print("Too high. Try again:", guess)
        guess = input("\n\t\tEnter a number between 1 and 100: ")
        guess = int(guess)
        count = count + 1
        if guess == val:
            print("\nCongratulations! You got it in", count, "guesses.")
            break

    elif guess &lt; val:
        print("Too low. Try again:", guess)
        guess = input("\n\t\tEnter a number between 1 and 100: ")
        guess = int(guess)
        count = count + 1
        if guess == val:
            print("\nCongratulations! You got it in", count, "guesses.")
            break
    else:
        pass

print("")
The pythonic way to do count = count + 1 is count += 1
You don't need to repeat the 'guess again' part of the game twice:

if guess &gt; val:
    print("Too high. Try again:", guess)  
elif guess &lt; val:
    print("Too low. Try again:", guess)
guess = int(input("\n\t\tEnter a number between 1 and 100: "))
count += 1
if guess == val:
    print("\nCongratulations! You got it in", count, "guesses.")
    break

You don't need to add the extra if statement because if guess != val, then the loop with break anyway:

while guess != val:
    if guess &gt; val:
        print("Too high. Try again:", guess)  
    elif guess &lt; val:
        print("Too low. Try again:", guess)
    guess = int(input("\n\t\tEnter a number between 1 and 100: "))
    count += 1
print("Congratulations! You got it in", count, "guesses.")

I think print("randint is", val) was probably for testing purposes, but it's still in your code!
If you execute your program outside the command module (I'm not quite sure what it's called), the program will close immediately after it's finished; it's always best to give the user the choice of when to quit, so insert this at the end of your code:

input("Press enter to quit")

The first section is unneeded:

def main():`, `if __name__ == "__main__":

The first if statement is also unneeded:

if guess == val:

You don't need to define guess = 0 the first time.


So, all in all, my improvement of your code is:

import random
print("Time to play a guessing game.")
val = random.randint(1, 100)
count = 1    
guess = int(input("\n\t\tEnter a number between 1 and 100: "))
while(guess != val):    
    if guess &gt; val:
        print("Too high. Try again:", guess)    
    elif guess &lt; val:
        print("Too low. Try again:", guess)
    guess = int(input("\n\t\tEnter a number between 1 and 100: "))
    count += 1
print("\nCongratulations! You got it in ", count, " guesses.")
input("Press enter to quit.")