← Back to Community
##### Number Guessing Game
Profile icon
leesroxyy

Hi this is a very simple game, the user guesses a number between 1-20 and is told if it is too high or low. If anyone has an idea that I (a python beginner) can do or suggestions to make this better please tell me. Thanks

Voters
Profile icon
JefrySandoval
Profile icon
Profile icon
NoahYonas
Profile icon
ShoryaMalani
Profile icon
Coder100
Profile icon
LizFoster
Profile icon
leesroxyy
hotnewtop
Profile icon
LizFoster

Okay, I've got a few little edits you can make to improve this overall:

-change "to" into "too" (grammar matters!) Also, make sure to capitalize statements that you print.

• You can import random as r, to save space and time, as well as improve the code's readability.

• Space out if statements and variables. It looks less cluttered this way. Also, space out chunks of code that are separate, this makes finding code much easier.

• Finally (This is my personal preference, you don't need this if you don't want it), use double quotations instead of singles ("" versus ''), unless you need to.

Here is the fully fixed code:

import random as r

number = r.randint(1, 20)

guess = int(input("I am thinking of a number between 1 and 20. What is it? "))

while True:
if guess > 20 or guess < 1:
guess = int(input("That's not in my range! Guess a number between 1 and 20: "))
elif guess > number:
print("Too high!")
guess = int(input("Try again: "))
elif guess < number:
print("Too low.. ")
guess = int(input("Try again: "))
else:
print("You guessed it! Good Job!")
break
(I don't know why the indents don't show up, sorry -_- )

Profile icon
ShoryaMalani

@LizFoster
When you say use double quotations dont you mean " versus ' not "" versus ' as that would not work.

Profile icon
LizFoster

@ShoryaMalani
Yes, exactly. I did not intend for "" versus '' to be a bit confusing.

Profile icon
MatthewDoan1

@LizFoster
You can use three backticks ` which, on American QWERTY keyboards, is left of the 1. Wrapping your code in three backticks will apply the regular formatting.

For example:

```.css-19sk4h4{position:relative;}.css-1bu6gr6{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:relative;}.css-1n2m10r{padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1hwur6u{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1svvr0w{height:0;}.css-rk73ff{padding:var(--space-4);padding-left:var(--space-4);padding-right:var(--space-2);font-family:var(--font-family-code);font-size:14px;line-height:var(--line-height-small);overflow-x:auto;tab-size:2;word-break:break-word;white-space:break-spaces;overflow-wrap:anywhere;}```import random as r

number = r.randint(1, 20)

guess = int(input("I am thinking of a number between 1 and 20. What is it? "))

while True:
if guess > 20 or guess < 1:
guess = int(input("That's not in my range! Guess a number between 1 and 20: "))
elif guess > number:
print("Too high!")
guess = int(input("Try again: "))
elif guess < number:
print("Too low.. ")
guess = int(input("Try again: "))
else:
print("You guessed it! Good Job!")
break``````

https://repl.it/talk/learn/A-Quick-Guide-to-Replit-Talk-Markdown/7448

Profile icon
LizFoster

@MatthewDoan1
Oh, I suppose that would make things much more clear. Sorry.

Profile icon
ShoryaMalani

Hey I remember when I did this a while back and somethings that can be really nice when working with users are making sure no matter what they type the program will not crash.
This can be done using a try: block before using int(input so that things will run smoother if accidently type a letter. Using a try block with a while statement is usually the best approach.(actually it might not be that is just how i do it)
something else that would be nice is adding a colon and a space at the end of an input statement. like guess=int(input('Try again: '))
If you can also print("") in places that need spaces as this makes more sense. This can be after Too high.
Lastly adding a way to say hey I want to play this game x number of times and then add a scoreboard for each time you play that is simple like hey you took 10 try which is 3 better than your last attempt. And then at the end average all the numbers and say hey it takes you average 5 times to solve this puzzle.

Profile icon
ShoryaMalani

@ShoryaMalani
I started one of the improvments I talked about. If you like you could implement the others. Here is the https://repl.it/@ShoryaMalani/random-number-guessing-game