Started By
Message

Trouble with Loops in Python

Posted on 6/30/23 at 4:39 pm
Posted by bayoubengals88
LA
Member since Sep 2007
21062 posts
Posted on 6/30/23 at 4:39 pm
I'm reading Automate the Boring Stuff with Python, and everything was going well until I ran into control flow, specifically 'where loops'.

I'm not sure if it's just the time of day or if this stuff is actually difficult to wrap my head around...

Any helps or advice is appreciated

Edit: I meant to say while loop.
This post was edited on 6/30/23 at 5:17 pm
Posted by Korkstand
Member since Nov 2003
28997 posts
Posted on 6/30/23 at 5:13 pm to
A where loop?
Posted by Fat Batman
Gotham City, NJ
Member since Oct 2019
1555 posts
Posted on 6/30/23 at 5:14 pm to
you mean while loops?

wait til you find out about recursion lol.
This post was edited on 6/30/23 at 5:16 pm
Posted by bayoubengals88
LA
Member since Sep 2007
21062 posts
Posted on 6/30/23 at 5:16 pm to
I’ve got SQL stuck in my mind.
Yes, while!
Posted by Fat Batman
Gotham City, NJ
Member since Oct 2019
1555 posts
Posted on 6/30/23 at 5:19 pm to
may want to step back and explore if/else, booleans and truthy/falsey values in python. then im sure there is a decent video on youtube that breaks down while loops in python's syntax.
Posted by UltimaParadox
North Carolina
Member since Nov 2008
47238 posts
Posted on 6/30/23 at 6:10 pm to
What's your issue? Just the flow or the conditional?
Posted by bayoubengals88
LA
Member since Sep 2007
21062 posts
Posted on 6/30/23 at 6:49 pm to
If you give me an example of a simple while loop, I’ll let you know.
Posted by Hulkklogan
Baton Rouge, LA
Member since Oct 2010
43470 posts
Posted on 6/30/23 at 7:15 pm to
count = 0
while count < 10
print("LSU won a baseball natty!")
count = count + 1



This would print that statement 10 times (0 - 9). The loop would stop once you hit iteration 11 (count would be the number 10) because the conditional (count < 10) is then false.



I tend to use for loops
This post was edited on 6/30/23 at 7:59 pm
Posted by wheelr
Banned
Member since Jul 2012
5800 posts
Posted on 7/1/23 at 12:56 am to
Is the number even or odd, with the option to test another number or exit.

quote:

continue = True

while continue:
....value = int(input("Enter a number: ")
....if value % 2 == 0:
........print(f"{value} is even")
....else:
........print(f"{value} is odd")
....user_response = input("Would you like to test another number? y/n ")
....if user_response == "n":
........continue = False
........print("Exiting...")


Or an alternative way to do the last if condition

quote:

....if user_response == "n":
........print("Exiting...")
........break
Posted by TAMU-93
Sachse, TX
Member since Oct 2012
1072 posts
Posted on 7/1/23 at 7:34 am to
There are better ways to learn Python. I suggest you put down that book and pick up this class CS50s Introduction to Programming with Python.
Posted by bayoubengals88
LA
Member since Sep 2007
21062 posts
Posted on 7/1/23 at 11:17 am to
Thanks, I'm going to take a step back and just start CS50 from Harvard. Get a foundation from C first and then tackle Python.

Indecisiveness and ignorance has been the theme for me over the past few months as I've taken interest in Data Science and then Computer Science for the first time.

There's just so much out there.
This post was edited on 7/2/23 at 4:48 pm
Posted by UltimaParadox
North Carolina
Member since Nov 2008
47238 posts
Posted on 7/1/23 at 11:35 am to
As someone who comes from a c background... I hate python
Posted by alabamabuckeye
Member since Jun 2010
22247 posts
Posted on 7/1/23 at 12:42 pm to
I never have to use while loops, just for loops for now. I guess while loops would be useful for interactive prompts where it needs to keep checking if a condition is True.
Posted by UltimaParadox
North Carolina
Member since Nov 2008
47238 posts
Posted on 7/1/23 at 3:44 pm to
While loops are a fundamental building block in basically all programming language. So if you keep solving then you will need them.

Generally you are correct keep looping until some condition is true. Granted it gets more complicated down the road. And also you usually don't want a while loop running wide open checking for a condition due to performance reasons, but that can be language dependent
Posted by tokenBoiler
Lafayette, Indiana
Member since Aug 2012
4822 posts
Posted on 7/1/23 at 10:28 pm to
[ duplicate ]
This post was edited on 7/1/23 at 10:32 pm
Posted by tokenBoiler
Lafayette, Indiana
Member since Aug 2012
4822 posts
Posted on 7/1/23 at 10:30 pm to
quote:

Data Science and then Computer Science for the first time.
I'm a big fan of OCW from MIT. This looks like it might be a good starting point for you:
Introductory Programming

I think 6.0001 is a much better option than Harvard's CS50.

ETA: most OCW lectures and recitation sections are on YouTube, FWIW.
This post was edited on 7/1/23 at 10:36 pm
Posted by bayoubengals88
LA
Member since Sep 2007
21062 posts
Posted on 7/2/23 at 12:37 pm to
Why are MIT lectures better than Harvard?
Posted by tokenBoiler
Lafayette, Indiana
Member since Aug 2012
4822 posts
Posted on 7/2/23 at 11:40 pm to
quote:

Why are MIT lectures better than Harvard?
Part of my job involves bringing new users up to speed with scientific computing. I've gone through 4 or 5 online courses in Python and R (topics I'm already familiar with) just to help me figure out effective ways to teach beginners.

When I went through the CS50 set, it covered (IMO) too many topics superficially -- it went through several different languages. The MIT course sticks with Python, and uses it to introduce fundamental CS topics, and programming idioms that are language-agnostic.

I think sticking with one language for that is easier to get into and retain for the online students. CS50 just seemed too scatter-shot for me.

That was a few years ago; I haven't looked at it recently.

CS50 seemed to me to be a class for someone who has to take what might be their only computer course; 6.0001 seemed to be a class for people who wanted to start learning CS with no prior experience.
This post was edited on 7/3/23 at 4:08 pm
Posted by bayoubengals88
LA
Member since Sep 2007
21062 posts
Posted on 7/3/23 at 8:46 am to
That’s excellent. Thank you for a truly authoritative stance on the matter.
Posted by alabamabuckeye
Member since Jun 2010
22247 posts
Posted on 7/3/23 at 9:21 am to
I took CS 1301 (Intro to Python) from Georgia Tech before starting my Masters and found it pretty useful too FWIW
first pageprev pagePage 1 of 2Next pagelast page

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on X, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookXInstagram