Number Guessing Game

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

You are viewing a single comment. View All

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: "))
print("You guessed it! Good Job!")
(I don't know why the indents don't show up, sorry -_- )


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


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


@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:


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