Here are a couple of improvements I think you could make (IEatBabels touched on a lot of these, but I'd like to expand on some areas and add my own phrasing):
- Why is
tries a list? It would easier and make more sense to just let
tries = 0 starting out, and then increment tries (
tries += 1) each time the user input an incorrect number.
- Since you don't have any other functions in your program, I wouldn't even bother with making a
Guess function. Just write the code directly in the program.
- You may have just been debugging your program, but you're showing the user the number they're supposed to guess by writing
- What happens if the user decides to be cheeky and entire some gibberish (i.e. something that's not a number)? Your program is going to raise an exception when it tries to convert the input to a number. What you should do is wrap the code under your loop in a
try/except block. That way, whenever the user inputs gibberish, you can prompt them to entire a valid number.
- I'm not sure why you have the
else block after your
if/elif statements? If the user input isn't equal to the number, and it isn't less than the number, and it isn't greater than the number, what else could it be? Nothing! So there's really no need for the
else statement there. You might've been trying to use the
else statement in cases where the user input wasn't valid. This won't quite work though. If that's what you were trying to do, see my last point.
- Convert the user input to an integer once, and store it in a variable. This saves time and looks cleaner than converting the user input each time you want to test it.
- I'm not sure why you're using recursion here? Recursion is an excellent tool, but oftentimes
for loops work much better. I think your program is one of those cases. Just use two loops - one loop for asking the user if they'd like to play again, and one loop for the actual game.
- I noticed you used flags to break out of our loops. This is a fine method. But I'd prefer here to just use
break is a statement which tells Python to immediately jump out of the loop it's currently in. It has the same effect as setting flags to
- You were pretty good about this in your program, but always make sure to use descriptive variable names, and write clear, explicit code. This makes your code clean and self-documenting, and it lets you come back to it months from now and quickly understand what it does and how works.
Here's how I'd re-write your program, with the above suggestions incorporated, and some formatting, logic, and naming improvements (also IEatBagels made an excellent point about whitespace. Make sure you take this to heart!):
print("We are going to play high and low, the avabile numbers are from 1 to 100 included.")
tries = 0
number_to_guess = random.randint(1, 100)
user_input = input("What is your number? ")
guess = int(user_input)
print("That's not a valid number! Try again.")
if guess == number_to_guess:
print("You have won!")
elif int(user_input) < number_to_guess:
print("You need to give a higher number")
tries += 1
elif int(user_input) > number_to_guess:
print("You need to give a lower number")
tries += 1
print("It took you " + str(tries) + " turns to guess")
still = input("Do you want to play again?")
if not (still == "yes" or still == "YES" or still == "y" or still == "si"):