r/PythonProjects2 • • 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?

Post image
91 Upvotes

31 comments sorted by

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

4

u/watsupwityahboi Aug 19 '22

Do you mean the 5th line or the line that defines number of guesses as 5?

10

u/fmpundit Aug 19 '22

Line 9 needs to be defined outside the loop

6

u/watsupwityahboi Aug 19 '22

And leave the subtraction where it is?

11

u/baconmaster687 Aug 19 '22

Yes. Anything inside the loop is going to keep happening. So right now every time it loops it sets the attempts to 5, then subtracts 1. What you want it to do is set it to 5 one time (so outside the loop) and then subtract 1 every loop

3

u/Real-Edge-9288 Aug 19 '22

well the 5th line is where the loop starts... so he meant the line where...

2

u/Areum_Fanny Aug 19 '22

Take out line 9. Put it outside for loop. Not sure what you are trying to do. Need the whole code. Also why do u need for loop?

8

u/futureNOW_ Aug 19 '22

I think they are just trying to learn.

5

u/fishypoos Aug 20 '22

The other user said it, it’s their first day give them a break lol

1

u/Areum_Fanny Aug 20 '22

They asked what they were doing wrong lol

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

u/[deleted] 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

u/[deleted] Aug 19 '22

You keep setting the var back to 5 every time the loop iterates!

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

u/[deleted] Aug 19 '22

Number of guesses s = 5 needs to be above the loop

1

u/Mundane_Eagle4220 Aug 20 '22

Haha! I am really sorry. But i cant see the question ? :)))

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

u/[deleted] 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!