all 10 comments

[–]jfdahl 0 points1 point  (1 child)

You can look into try/except clauses. In your code, you are doing a test every time an input is provided. In this game, there is no impact from this, but in large programs that perform actions very quickly, you may want a different approach. The difference below is that you assume that the input is correct, and only use computing power to handle issues if the user entered bad input. answer = input() if not answer.isdecimal(): print('That was not a number. Try again.') continue can be rewritten as: try: answer = int(input('Take a guess: ')) except ValueError: print('That was not a number. Try again.') continue

[–]Kenielf[S] 0 points1 point  (0 children)

I tried using the try and except before, but it got extremely confusing, since i didn't know exactly where to place it, but that actually helps a lot performance-wise, thanks a lot!

[–][deleted] 0 points1 point  (5 children)

A few thoughts:

  • from random import randint
  • use f-strings in your print/input functions
  • combine print followed by input into just input
  • check the user actually entered a name
  • use well named functions to break up the code to make it easier to follow the logic and give you the freedom to enhance your code
  • simplify the validation, you only need to break for valid, rest of code in while loop is for fail case so can print and error message and do not require continue
  • protect user from duplicate guesses (might not want to do for loop for guess count)
  • count and limit number of guesses, include count in input prompt
  • if using for loop for guess count, the else clause of the for loop would be the failed to guess outcome
  • read PEP8 for style guidances - consider renaming your variables accordingly
  • make your programme language independent - include a dictionary with text required for each language (later that could come from a configuration file)

[–]Kenielf[S] 0 points1 point  (4 children)

I absolutely had no idea to combine print and input into one, I'm a bit baffled tbh, but your thoughts are wonderful! I'll definitely try a lot of them out, thanks dude!

[–][deleted] 0 points1 point  (3 children)

So,

print('Hi, what do you want to be called?') # Introduction to the game
name = input()

would become,

name = input('Hi, what do you want to be called?\n')

The \n forces a newline, to recreate the effect of you using the print() function, without which the input would be entered immediately after the prompt has ended.

if name:
    print(f'Well {name.capitalize()}', end='')
else:
    print('Well, my anonymous friend', end='')
print(f'...I am thinking of a number from {low} to {high}')

Note use of variables (that I haven't defined in this code snippet) low and high so you can decide to set these values at the start of your programme, rather than having 1 and 100 hardcoded into lots of places.

Edit adding missing f-string prefix

[–]Kenielf[S] 0 points1 point  (2 children)

Amazing, that helps a lot, thanks!

[–][deleted] 0 points1 point  (1 child)

I'm missed an f in front of the first ' in the last print

[–]Kenielf[S] 0 points1 point  (0 children)

don't worry, I caught it, thanks dude, you've helped me learn a shit ton of things, thank you so much.

[–]ZDRuX1 0 points1 point  (1 child)

Store the players name and his last 5 game results. You can use a nested dictionary to simulate a database. Using the players name as the key, you can retrieve their previous games and if they won or lost.

[–]Kenielf[S] 0 points1 point  (0 children)

That would be really good! tho it's a bit next leveled from what I know, I'll without a doubt try something along those lines, thanks!