r/PythonProjects2 • u/watsupwityahboi • Aug 19 '22
A few hours into learning programming (with any language). No matter how many times I guess, it always says “You have 4 guesses left.” What am I doing wrong?
6
u/watsupwityahboi Aug 19 '22
Thanks you guys are awesome 😇 Sorry for the late response. I was tinkering
3
u/MrMagik14 Aug 19 '22
Awww I knew the answer and wanted to help explain but I was too late. I was hoping answering this question about programming would be the spark to get me out of the food industry 😭
3
u/watsupwityahboi Aug 19 '22
The spark is in your attempt! I’m trying to learn to switch careers as well; operations to data analytics. Keep pushing bro and I’ll do the same! 🙌
1
u/MrMagik14 Aug 19 '22
I had a customers suggest I should try data analytics too. Maybe it’s. A sign. I appreciate the positive vibes and encouragement my boi!
8
u/Unusual-Tree-1559 Aug 19 '22
Put “number_of_guesses = 5” before the for statement
ETA - you should also get rid of the break and use elif statements instead to make it flow better.
3
Aug 19 '22 edited Aug 19 '22
Put “number_of_guesses = 5” above “for n in number_list”
Right now you’re resetting the “number_of_guesses” back to 5 every time the for loop runs through an iteration.
I would recommend looking up what a variable/function “scope” is. Lots of good information out there on the internet about it.
2
u/Worldly-Cycle1925 Aug 19 '22
For each iteration in the 'for' loop, you are redefining number_of_guesses to 5. Then you are subtracting 1, which is why it keeps printing 4. You want to only set number_of_guesses once, so move the number_of_guesses = 5 line to between the function definition and for loop.
2
2
u/Kevinw778 Aug 20 '22
Additionally, instead of having a separate if / else condition for each number of remaining guesses, you can have it print the current number of guesses based on the variable that you're already using to track the remaining guesses.
You can do this very cleanly using f-strings :)
eg:
print(f'Number of guesses remaining: {guesses_remaining}')
1
u/atomicsoapss Aug 20 '22
Was going to say that, makes the code a lot better to read/write. Just put it under a >0 condition and you’re good to go.
1
1
1
1
u/KingCider Aug 20 '22
Idk if anyone mentioned this and I could be wrong, but I'm pretty sure your number_list only has 1 element. The way you wrote it it is a list of lists of numbers from 1 to 100. You define put number_list = range(1, 101) instead.
-1
u/Its_a_Feature-nub Aug 20 '22
Is this a troll?
2
u/watsupwityahboi Aug 20 '22
🙁
1
Aug 20 '22
It’s alright OP! Everyone has gotta start learning somewhere! Keep on practicing and learning.
Some advice: Actually doing the code is super fun but go on YouTube and watch some videos that go into depth about the processes on it. Like explaining maybe arrays etc. it’s good to code but also to know the more in-depth workings as well!
40
u/puhalalu Aug 19 '22
You're defining the number of guesses equals 5 each loop then subtracting one. Put the 5 line outside the loop