r/PythonLearning • • 5d 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?

159 Upvotes

44 comments sorted by

•

u/Sea-Ad7805 5d ago

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

→ More replies (2)

5

u/Dracunculous 5d 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.

5

u/RegularBother614 5d ago

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

1

u/ProgramBrave718 5d 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

2

u/ProgramBrave718 5d 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 5d ago edited 5d ago

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

1

u/ProgramBrave718 5d ago

Do u use it later?

1

u/RegularBother614 5d ago

Yes, on L32 to L37

1

u/PureWasian 5d 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.

4

u/SoloEnder 5d ago edited 5d 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 5d ago

U mean line 16?

1

u/SoloEnder 5d ago

Yeah sorry, i shouldn't have commented lol.

5

u/ProgramBrave718 5d ago

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

4

u/Dracunculous 5d ago

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

1

u/ChaoticHippo 5d 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.

0

u/SoloEnder 5d ago

Alright, sorry for the low quality comment 😓

2

u/ChaoticHippo 5d ago

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

3

u/TemperatureProper275 5d 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 5d 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 5d 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 5d 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 5d 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 5d ago

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

1

u/RegularBother614 5d ago

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

0

u/[deleted] 5d ago

[deleted] — view deleted comment

2

u/TemperatureProper275 5d ago

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

1

u/burntoutdev8291 5d ago

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

12

u/PureWasian 5d 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.

2

u/RegularBother614 5d ago

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

2

u/One_Smoke6858 5d ago

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

So good job 

5

u/ChaoticHippo 5d 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 5d 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.

1

u/aroberge 4d ago

What do you guys think?

I think that you need to learn to post code on reddit (simply indent your code by an extra 4 spaces and copy-paste here) rather than posting images.

I can't read code in images.

1

u/Particular-Age3726 4d ago

I when In invest

1

u/hellofr4552 3d ago

Well it can be improved

1

u/Abirdthatsfallen 3d ago

You gonna give them advice or just say “flawed” with no explanation? They want to improve, that’s why they’re here, help them.

2

u/hellofr4552 3d ago

Buds it's not that I don't want to but my gaming program involves other method so without watching this particular video through which op has learnt from i wouldn't want to give some wrong advice.

If he doesn't know the way to do those methods which requires multiple logic then I would be just confusing op

1

u/Abirdthatsfallen 3d ago

Fairs, my bad

1

u/hellofr4552 3d ago

That being said if you are expert in python you should try to help op

1

u/shingomada012 1d ago

oh very nice program. I'm also studying Python Programming on Udemy and the vaste YT. Bro Code have a good teaching skills.
one question: which app are u using on? (on that print screen) (seems a androidOS, am I right?)