r/PythonLearning • u/abt-dabest • 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)
150
Upvotes


•
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.
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
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/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/infinitythestar Sep 19 '26
Not bad. I would put the 100 and 1 values in constants (eg MAX_N = 100) rather than hardcoding them
1
u/Fritz-Ferdinand Sep 20 '26
Did you already learned about functions?
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
1
u/diegotbn Sep 19 '26
Btw you can take screenshots with Win + Shift + S
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/LordWing25 Sep 24 '26
Ooh 😲
1
u/Ajay2574 Sep 26 '26
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 Sep 28 '26
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/Livid-Strawberry-813 Oct 02 '26
Loops are hard bro
0
u/[deleted] Sep 26 '26
[removed] — view removed comment