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
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 -_- )
@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:
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
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
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 -_- )
For example:
https://repl.it/talk/learn/A-Quick-Guide-to-Replit-Talk-Markdown/7448