The comments on these lines really don't tell anything new: if guess = number: # Correct answer The recommended way is: while not correct: Comparison with boolean valuesĭon't use = or != with boolean values. Since the only place where you expect something to go wrong is the guess = int(guess) line, it would be better to wrap only that in the try-except. You have a very large try block here: try: So you can pull it up, out of the if-else: tries += 1 The value of tries will be increment no matter what. Print("\nWay too high! Try a number 10 or lower.") Another benefit is that you can drop the str(.) wrapper, format will automatically take care of that for you. By removing + varname + from the middle of strings, the text becomes a bit easier to read. The recommended way is: print("\nYou have guesses left.\n".format(remaining)) Instead of: print("\nYou have " + str(remaining) + " guesses left.\n") Try solution is almost always to create a new function, for example: def print_remaining_guesses(tries): Try to avoid pressing Control V while programming. Most probably you copy-pasted it every time you needed. Print("\nYou have " + str(remaining) + " guesses left.\n")
Code hs 2.7.9 code#
This piece of code appears multiple times: remaining = 5 - tries You might even want to make these constants by uppercasing the names. The solution is simple: give the magic numbers meaningful names, for example: lower_limit = 1 If the number 5 is used for other than the purpose of maximum number of tries, then the change will be especially difficult, as you would have to review each occurrence to verify its purpose. If you wanted to change the maximum number of tries, you would have to manually search and replace all occurrences of 5. The numbers themselves don't carry a meaning, but they are special to the program. Thanks in advance!ġ, 10, and 5 are so-called magic numbers in the code: they are duplicated in many places. I'd love to get some feedback on what I have here. # Exited loop with tries left, must have guessed correctly Print ("My number was " + str(number) + ".") Print ("\nSorry, you ran out of guesses!") Print ("\nThat wasn't a number! Try again.") Print ("\nMake sure you guess a number 1 or higher!") Print ("\nYou have " + str(remaining) + " guesses left.\n") Print ("\nWay too high! Try a number 10 or lower.") While correct = False and tries number: # Guess too high # and they haven't already guessed correctly. # Ask the user for their guess, as long as there are tries remaining Print ("I'll give you 5 chances to guess it.\n") Print ("I'm thinking of a number between 1 and 10.") (I also added a limited number of tries to the game, so the user can't try 1-10 in order until they guess right.) from random import randint You will get to keep guessing until you guess the number." The user inputs a number,Īnd the computer will tell you if you are too high, or too low. "The computer randomly generates a number. So while I know my code works, I'm not sure if I've written it in the best way. I found the following exercise for a "Guess my Number" game on DaniWeb, but the linked source code file is no longer active. Just looking to get some advice on how I can write better code.