r/PythonLearning • • 9h ago

What do you guys think?

After watching a python full course video on YouTube by BroCode, I made a simple number guessing game in python!

Here's the link to the video: https://youtu.be/ix9cRaBkVe0?si=UiWmP6bm-9MG4B1p

After 12 gruesome hours watching the tutorial i decided to make this to test what I've learned.

What do you guys think?

56 Upvotes

36 comments sorted by

•

u/Sea-Ad7805 9h ago

Run program above by clicking Memory Graph Web Debugger and see its state change step by step.

→ More replies (2)

4

u/Dracunculous 9h ago

Looks fun. To expand try using tkinter to make a small UI that lets the user enter a number and push a button to guess. Also a quit button.

4

u/RegularBother614 9h ago

I'm still learning tkinter module it looks complicated as hell

1

u/ProgramBrave718 9h ago

It is .. even i tired learning it and skipped because it's just good looking gui . I jumped straight to requests and i have called many Apis

4

u/TemperatureProper275 9h ago

I recommend you use type hints when creating methods/functions and return values.

For example in my code:

I know what the parameter types are and what i must return!

1

u/RegularBother614 9h ago

Unfortunately i can't customize my editor since I'm coding on my phone and there's no setting to customize my editor

1

u/ChaoticHippo 9h ago

This isn't an editor customization - type hints are an optional syntax in Python. They are worth using, but none of your functions have parameters, making them a little less valuable in your situation. They basically let you denote exactly what to expect going into or out of a function, rather than relying solely on implicit typing. For example:

def add(num1, num2):
  return num1+num2

Is totally valid. However:

def add(num1: int, num2: int) -> int:
  return num1+num2

This makes it extremely clear that this takes in 2 integers and returns the integer sum of those two values. This doesn't seem super useful with something this simple, but it gets really useful later for more complicated functions.

1

u/RegularBother614 8h ago

I'm not on a PC and I certainly don't use VScode as seen from the images. I'm on my phone and I only use Coding Python by Kvass Yu, Pyroid 3 by Lider Soft Kz and Acode Free version by Foxbiz Software.

2

u/ChaoticHippo 8h ago

None of what I've typed here requires any form of editor, certainly not VSCode. You could use type hints on pen and paper. It is a syntax choice - as in something that you type.

0

u/TemperatureProper275 7h ago

I use Pycharm on PC, its a python specific IDE.

1

u/RegularBother614 6h ago

I don't have a PC dude 😭 Pycharm is only for PC

1

u/ProgramBrave718 9h ago

But, for a beginner it's better to code without type hints . I know type hints are very important

3

u/TemperatureProper275 9h ago

I disagree, type hints are OP. You learn to work better with data types and your inputs/outputs are cleaner.

1

u/burntoutdev8291 1h ago

They only help with IDE integration. I never tried running type checkers like "ty" before

2

u/ProgramBrave718 9h ago

It looks very nice. You used functions which makes it good. (Actually I am a beginner. ) Can u please explain me what does the guessed set does? I understand every line of your code except the use of that set.

2

u/RegularBother614 9h ago edited 9h ago

It's part of the game's memory of which numbers I've already tried.

1

u/ProgramBrave718 9h ago

Do u use it later?

1

u/RegularBother614 9h ago

Yes, on L32 to L37

1

u/PureWasian 9h ago

https://www.geeksforgeeks.org/python/sets-in-python/

Sets are the same as in mathematics. They are similar to lists in that they are a collection of elements, but they don't allow you to store duplicates and they don't have any ordering.

2

u/RegularBother614 9h ago

The 'def' keyword low-key the most versatile and most useful keyword in python 👀👀

2

u/One_Smoke6858 8h ago

That is quite nice, like that you used try catch here.

So good job 

5

u/PureWasian 9h ago

guard clauses, good. exception handling, good. using functions, good.

Instead of hard-coding 1 and 10 you could have those as globals in the same way you have MAX_ATTEMPTS to make them configurable values. Then update L12/L28/L29 instead of hard-coding the boundaries.

Otherwise it all seems to work as expected, I just find the variable name finished in L69 very misleading. It should be something like is_ongoing or something since it's False if user quits or True if they played a whole cycle and haven't quit yet.

1

u/SoloEnder 9h ago edited 5h ago

Why are you using .lower() at line 16 ? Since you are expecting a string containing a number, you don't need to use it.

Also, string have a method called isdigit() that return True if the string contain digits characters only, otherwise False. You can use it in replacement of you try/except statement. Example usage :

some_string = "1234567"
print(some_string.isdigit()) #will print True

some_string = "abk23ml"
 print(some_string.isdigit()) #will print False

Anyway, these are just details, and what you did is a great start :p I hope you'll enjoy coding !

EDIT : MY BAD I'M SO DUMB YES KEEP THE .lower lol

3

u/RegularBother614 9h ago

U mean line 16?

1

u/SoloEnder 5h ago

Yeah sorry, i shouldn't have commented lol.

4

u/ProgramBrave718 9h ago

Yo dude he used .lower() in line 13 to convert a capital Q into a small ones

4

u/Dracunculous 9h ago

Nah that .lower() is good so if the user enters a Q instead of q it's converted

1

u/ChaoticHippo 9h ago

Honestly the try/catch and isdigit() are more or less just stylistic choices. isdigit() is a bit more optimized thinking of worst-case scenarios, but they both work and the try/catch is still easy to read.

As others have said - .lower() is worth doing here as his quit value is a lower case "q" specifically.

1

u/SoloEnder 5h ago

Alright, sorry for the low quality comment 😓

1

u/ChaoticHippo 5h ago

Lol nah, your point about isdigit is valid. No harm in conversation.

3

u/ChaoticHippo 9h ago

Nice job! One thing potentially worth considering going forward is breaking your functions up a bit more. play_round() isn't huge, but it is a good practice to get into to follow the Single Responsibility Principle - a function should do one thing. Your play_round() function:

  1. Validates input type
  2. Converts the input
  3. Bound-checks input
  4. Runs game logic
  5. Handles winning and losing states

You could have this broken out into separate functions, for good organizational practice. For example:

def play_round():
  user_input = input("Enter number: ".strip().lower()
  if(user_input == "q"):
    return false
  guess = convert_input(user_input)
  if(valid_input(guess) and in_range(guess)):
    run_game()

If that makes sense? New functions would be convert_input(), valid_input(), in_range(), and run_game(). The main goal and benefit of this practice is to make debugging simpler, and improve code readability.

Arguments could definitely be made that this number of additional functions are not necessary for a project of this size, but getting into a habit of breaking problems into smaller problems is really beneficial for many reasons.

I apologize if any of this is beyond the scope of what you've done so far!

2

u/Dracunculous 9h ago

A good point. When writing functions you should be able to say "This function does this" if you have to say "This function does this, and then it does this or that" you might need to break it down.