2
\$\begingroup\$

This is a simple guessing game I have made, an improvised one. I will do a GUI version of this shortly.

I'm looking for comments and interesting code ideas.

import random sec_no = random.randint(0, 10) ncount = 0 att = 5 while True: sec_no = random.randint(0, 10) ncount = 0 att = 4 minp = input(''' 1-start 2-help 3-quit ''') if minp.lower() == 'start' or minp == '1': while ncount != 4: att -= 1 try: uinp = int(input('Guess the no:')) if uinp == int(sec_no): print('correct!!!!') break else: print('incorrect!!!') print(f'attempts left:{att}') except ValueError: print('invalid value!!!') print(f'attempts left:{att}') if ncount == 3: print('You lose!!') print(f'correct number is :{sec_no}') break ncount += 1 elif minp.lower() == 'quit' or minp == '3': while True: print('are you sure you wanna quit? [Y/N]') linp = input('>') if linp.lower() == 'y': print('Thanks for playing!!') exit() elif linp.lower() == 'n': break else: print("I don't understand that!!!") elif minp.lower() == 'help' or minp == '2': print('''You will be given 3 attempts,within these attempts you'll have to guess the right number from 0-10 good luck!!!''') else: print("I don't understand that!!") 
\$\endgroup\$
2
  • 3
    \$\begingroup\$ The indentation here is off. Typo? \$\endgroup\$ Commented Jan 3, 2020 at 3:20
  • 1
    \$\begingroup\$ Welcome to CodeReview. I find it easier to create code blocks using lines just containing ~~~ before and after: you don't have to touch indentation. Please heed How do I ask a Good Question?, improving the title of this post, too. \$\endgroup\$ Commented Jan 3, 2020 at 4:42

2 Answers 2

2
\$\begingroup\$

There are few improvements you can do:

  1. Remove the first sec_no = random.randint(0, 10). You are already initializing it inside of the main while loop.
  2. Create a variable that contains minp.lower(). Instead of calculating it over and over - keep it in a variable and avoid code duplication.
  3. Get rid of ncout or att. They are practically saving the same thing, keep only att and change the while and if condition to att > 0.
  4. Add some comments - File comments and function comments.
  5. General python tip: do not write code in main, Use functions. Function for each case, function for main loop, function to check if string is int and the __main__ should look like this:
if __name__ == '__main__': main_function() 

Except for that - good logic, nice code :)

\$\endgroup\$
2
\$\begingroup\$

You could implement a hint to the answer so you can broaden the range of numbers. This is just a quick example.

import random num = random.randrange(1, 100) lives = 5 print(''' Welcome to the guessing game. Guess a number between 1-100.\nYou have {} lives '''.format(lives)) while True: try: if lives == 0: print('You died!\nThe number you were looking for is {}'.format(num)) break guess = int(input('Guess: \n')) if guess == num: print('You win!') break if guess < num: lives -= 1 print('Number is higher...\nlives: {}\n'.format(lives)) if guess > num: lives -= 1 print('Number is lower...\nlives: {}\n'.format(lives)) except ValueError: print('Numbers only!') 

The result:

Welcome to the guessing game. Guess a number between 1-100.You have 5 lives Guess: 50 Number is lower... lives: 4 Guess: 30 Number is lower... lives: 3 Guess: 25 Number is lower... lives: 2 Guess: 10 Number is higher... lives: 1 Guess: 16 Number is lower... lives: 0 You died! The number you were looking for is 13 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Really nice, I usually like to use fstrings instead of format. Just notice that this works for python3 and above. \$\endgroup\$ Commented Jan 5, 2020 at 20:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.