I recommend you to go through this topic. You will se a lot of useful information there:
My suggestions for your code:
def generate_number():
r_number = random.randint(1, 100)
return r_number
- Missing docstring — What about
"""Generate a random number to be guessed."""? - Shortening of variable names could lower readability. — why not
random_number? - Do you need the variable at all? — If you understand what
random.randint()does, return the result directly without storing it into a variable.
def easy_or_hard_game(tries):
for trie in range(tries):
print(f"you have {tries -trie} attempts remaining to gues the number.\n")
gues = int(input("Make a gues: "))
if gues == number:
return guesed_number + 1
if gues < number:
print("Too low")
else:
print("Too high")
- Missing docstring again.
- Function name — there is just a single implementation in the program so why having
easy_or_hardin the name? Maybe you would like to make multiple games in the program then it could benumber_guessing_gamebut part of the logic is in your main program so it would need some restructuring. - Spelling — In your screenshot I noticed that you use a spell checker. Why do not you check its suggenstions? “trie” is correctly “try” or “attempt”, “gues” is correctly “guess”.
- Global variable
number— Normally it is best to avoid global variables. Doing so is an extremely useful practice before writing more complex programs! Pass the number as a parameter of the function instead. - Why do you use the global variable
guesed_number? In your program it could contain only value1and nothing else when this function is executing. It looks like you use it just as an indication of whether the number was guessed correctly or not. You should do just:return True
guesed_number = 1
- What does that number mean? See above that this variable not needed at all. (Unless you want to later extend the program in a way I do no understand.)
guesed_number = easy_or_hard_game(chances)
- You duplicate the same code in both the branches of the
ifstatement. Put it just once afterif/else. - After that simplification you can maybe skip storing the result in a variable and use it directly in the the following
ifstatement.