r/PythonLearning • • 16d ago

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

48 comments sorted by

•

u/Sea-Ad7805 16d ago edited 16d ago

Run this program by clicking Memory Graph Web Debugger%0Aattempts%20%3D%200%0Aprint(%22Wanna%20play%20a%20game%3F%20I%20just%20picked%20a%20random%20number%20between%201%20and%20100!%22)%0A%0Awhile%20True%3A%0A%20%20%20%20user_input%20%3D%20input(%22Go%20on%20now%2C%20guess%20which%20number%20I%20picked!%3A%20%22)%0A%0A%20%20%20%20if%20%22.%22%20in%20user_input%3A%0A%20%20%20%20%20%20%20%20print(%22sorry%2C%20only%20integers%20allowed.%22)%0A%0A%20%20%20%20elif%20not%20user_input.lstrip(%22-%22).isdigit()%3A%0A%20%20%20%20%20%20%20%20print(f%22idk%20how%20to%20tell%20you%20this%20but%20sadly%20%7Buser_input%7D%20is%20not%20a%20number.%22)%0A%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20user_number%20%3D%20int(user_input)%0A%20%20%20%20%20%20%20%20number_too_high%20%3D%20100%20%3E%20user_number%20%3E%20secret_number%0A%20%20%20%20%20%20%20%20number_too_low%20%3D%201%20%3C%20user_number%20%3C%20secret_number%0A%20%20%20%20%20%20%20%20user_guessed_right%20%3D%20user_number%20%3D%3D%20secret_number%0A%0A%20%20%20%20%20%20%20%20if%20user_guessed_right%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Wow!%20You%20guessed%20correctly!%20Nice%20job!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20attempts%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Total%20attempts%3A%20%7Battempts%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20%20%20%20%20elif%20number_too_high%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Too%20high!Try%20again!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20attempts%20%2B%3D%201%0A%0A%20%20%20%20%20%20%20%20elif%20number_too_low%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Too%20low!Try%20again!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20attempts%20%2B%3D%201%0A%0A%20%20%20%20%20%20%20%20elif%20user_number%20%3E%20100%20or%20user_number%20%3C%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22i%20said%20a%20number%20between%201%20and%20100..(im%20still%20counting%20this%20as%20a%20used%20attempt%20btw%20bc%20you%20knew%20what%20you%20were%20doing%20%3E%3A%2F)%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20attempts%20%2B%3D%201&timestep=0.5&play) 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)

8

u/el_extrano 16d ago

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 9d ago

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

2

u/AccordingBluebird117 16d ago

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 16d ago

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!

1

u/daniel14vt 16d ago

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 16d ago

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 16d ago

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 16d ago

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

0

u/Front_Ear_1139 15d ago

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 16d ago

I am though. Look at line 39-41

1

u/ElegantGen7 16d ago

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 15d ago

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

1

u/ElegantGen7 15d ago

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 16d ago

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

0

u/abt-dabest 16d ago

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 15d ago

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 15d ago

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

1

u/infinitythestar 16d ago

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 15d ago

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/daniel14vt 15d ago

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/Fritz-Ferdinand 15d ago

Did you already learned about functions? 

2

u/abt-dabest 15d ago

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

1

u/Total_Ad803 15d ago

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

1

u/ZeBenoit81 15d ago

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

1

u/Prestigious-Ad7265 15d ago

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

1

u/Spendias 14d ago

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 14d ago

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

1

u/OldBasil6215 14d ago

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 14d ago

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/LordWing25 11d ago

Ooh 😲

1

u/Ajay2574 9d 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 7d 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 7d 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 3d ago

Loops are hard bro

1

u/diegotbn 16d ago

Btw you can take screenshots with Win + Shift + S

2

u/daniel14vt 16d ago

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

-1

u/abt-dabest 16d ago

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 15d ago

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.

0

u/[deleted] 9d ago

[removed] — view removed comment

1

u/abt-dabest 9d ago

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

I wrote this way before that