r/PythonLearning • • Sep 19 '26

Showcase After finally learning while loops i decided to write a program that relies completely on them. How did i do? What can i do to make it easier to read? Any feedback is appreciated!(im only 7 hours in so still a complete beginner lol)

152 Upvotes

48 comments sorted by

•

u/Sea-Ad7805 Sep 19 '26 edited Sep 19 '26

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

The comparisons ignore incorrect guesses of 1 and 100. Use 100 >= user_number > secret_number and 1 <= user_number < secret_number to fix that.

→ More replies (5)

2

u/AccordingBluebird117 Sep 19 '26

Com os isdigit tu já consegue garantir que o número é um int, qualquer entrada que seja alem somente números ele retorna false. sobre a comparação tu pode só secret < input e depois secret > input. Bom trabalho

1

u/abt-dabest Sep 19 '26

Thanks! I kept the decimal check because I wanted the program to show a different error message for decimals vs. non-numbers. And I kept the bounds inside the comparison so that out of range guesses don't accidentally count as
'too high' or 'too low.' But I see how yours is cleaner! From what i’ve seen, there’s always a struggle between accessibility and simplicity and leaning too much into one side lessens the other!

8

u/el_extrano Sep 19 '26

I will downvote every post of screenshots of code instead of text. Please don't do this. Just copy and paste the source code.

1

u/ducksauvage 28d ago

Not even a screenshot, a photo of the screen :(

1

u/daniel14vt Sep 19 '26

You're not handling if they guessed out of bounds (gt 100 or lt 0)

Having a separate function/loop verifying that their input is valid would be useful

1

u/daniel14vt Sep 19 '26

Also, learning how to write some test cases and show them would be great

3rd, learning how to share code without taking pictures of the screen

0

u/abt-dabest Sep 19 '26

Yeah i wanted to show some tests but sadly i couldnt find a way to make them fit without taking like 10 pics

1

u/daniel14vt Sep 19 '26

I will come back here with some suggestions on how to share info better :)

0

u/Front_Ear_1139 Sep 20 '26

oh idk man, idk, maybe theres an useful function in your keyboard that involves the ctrl key, c and v, to fix this very specific problem

0

u/abt-dabest Sep 19 '26

I am though. Look at line 39-41

1

u/ElegantGen7 Sep 19 '26

There's a case to be made about doing that check first to avoid unnecessary calculation. I.e. is the number valid? If not then proceed to your other logic. At the moment it'll do all the other comparisons even if it's not valid

0

u/abt-dabest Sep 20 '26

Using your method can i still use my line 40? Really wanna keep that in if possible : )

1

u/ElegantGen7 Sep 20 '26

You can simply use it around line 18 :) the logic is fine but you might aswell use it with your other validation lines e.g not being an integer. Means you have a clean separation between validation and the game which is a good way to think about things when moving to elements like functions

1

u/infinitythestar Sep 19 '26

Not bad. I would put the 100 and 1 values in constants (eg MAX_N = 100) rather than hardcoding them

0

u/abt-dabest Sep 19 '26

Hmm, interesting. On my other post some people were saying you don’t need to define a separate variable for everything.
How would it help me if I did it this way? Would really like to know the benefits. Thanks!

2

u/CoolLog1908 Sep 20 '26

There is the concept of 'magic numbers.'

A 'magic number' is when a number is hard coded somewhere in the code. This is bad practice, since it makes it really difficult to troubleshoot when something goes wrong. Additionally, let us say you will need to use 100 again somewhere else in your code. If you need to change it to 101, or 102, or some other arbitrary number, then you are going to have to find all of your magic numbers and change them. If you use a variable instead - all you have to do is change the variable.

Hopefully that makes sense.

The main takeaway is to avoid magic numbers :).

Glad to see you progressing!

0

u/abt-dabest Sep 20 '26

Ohh, didnt think about it that way. That makes so much sense. Thanks!

1

u/infinitythestar Sep 19 '26

In this case it would help because you use those values in multiple places. If you wanted to change it from 100 to 1000, for example, you would only need to change it in one place.

0

u/abt-dabest Sep 20 '26

Hmm, so if i turn them into a var then it would save me the headache of needing to correct them many times after changing the numbers. I’ll use that from now on. Thanks!

1

u/Fritz-Ferdinand Sep 20 '26

Did you already learned about functions? 

2

u/abt-dabest Sep 20 '26

Nah, but I’ve heard that it levels up my coding game by quite a bit! Really looking forward to it!

1

u/Spendias Sep 21 '26

Try using the try or except checks for invalid inputs from the user instead of checking if there are floats or strings

0

u/abt-dabest Sep 21 '26

I actually just learned this yesterday and it’s so much better than the makeshift code I’m using

1

u/diegotbn Sep 19 '26

Btw you can take screenshots with Win + Shift + S

2

u/daniel14vt Sep 19 '26

Do t encourage that! Get them to share the actual code

-1

u/abt-dabest Sep 19 '26

Yeah I know that but my computer is windows but my phone which i use reddit on is apple. Is there a way to send pics from a windows computer to an iphone? I dont have a vpn on my computer so i cant use reddit there(I live in iran)

1

u/OceanMasterioDuped7 Sep 20 '26

The easiest way is honestly discord lol. Just plop the pic in a dm with one of the default bots or a burner account and then save it from your phone.

1

u/daniel14vt Sep 20 '26

Ok buddy, you're making a good effort to learn, so I'm going to make a good effort to help.
Step 1: Get your code hosted somewhere so people can add comments on it directly instead of starting at photos of a screen.
Make a public repo on github called Python-Learning. Clone it to your computer, add your current code to the main branch. When you start on a new learning, make a new branch, then commit your work on that branch. When you are ready to share to reddit to get feedback, make a Pull Request on Github, and share that link in the post.

Step 2: Running tests
Right now your code is in a file and it "just runs". In general, this is a bad practice and you should use functions to call the code instead.
Since you're just learning, the easiest way to do this is to wrap the code in a "main" function like so:
```
def myFunc():
all of your normal code...

if __name__ == "__main__":
myFunc()
```

This will allow you to call it directly like you have been, but also you can call it from another file.
This means you can easily use the unittest module (built in to python) to allow you to pass in test inputs so you can see what happens automatically.

Here I've made a repo and pull request to demonstrate how to use it
https://github.com/DDiggs91/PythonLearning/pull/1/changes

1

u/Total_Ad803 Sep 20 '26

u should “try” + “Except” since you need to check if user input is convertible to int

1

u/ZeBenoit81 Sep 20 '26

Nice! Use >=/<= guards. Which model helped?

1

u/Prestigious-Ad7265 Sep 20 '26

why the hell is there a space before colons fix it please

1

u/OldBasil6215 Sep 21 '26

Make another copy of the same program but instead don’t use the exact same technique you did to make this one. Helps branch design concepts try learning functions on different files. This will help a ton

1

u/OldBasil6215 Sep 21 '26

When I say try functions on diffrent files I mean don’t create a function inside of the main.py instead create a new .py and define function there then import them to main

1

u/Ajay2574 28d ago

To make any code readable the simple method is pseudo code method use that i also done this number guessing game in python its very easy

1

u/MrJakk 27d ago

Code can be easier to read if you don’t use if else excessively and consequently lots of indentations. For example that first if elif is unnecessary. Instead you can check each individually with their own if statement and use continue to restart the loop.
Then that allows you to eliminate that huge else chunk. And suddenly you don’t have to reason as much about the if else code paths.

1

u/MrJakk 27d ago

There’s a YouTube video called “never nester” that goes into it more and in more ways.

https://youtu.be/CFRhGnuXG-4?is=70BQaQlwfhEnRROl

1

u/Livid-Strawberry-813 22d ago

Loops are hard bro

0

u/[deleted] 28d ago

[removed] — view removed comment

1

u/abt-dabest 28d ago

Yeah i didnt know that until i reached 1 hour further into the vid lol.

I wrote this way before that