Learn Python with Me! Part 2
In my last post, I left off on adding recursion to my TotalPurchase program. In this article, I wrap up that program as well as create 2 more programs. I’ve decided to pick challenges in later chapters in Starting out with Python by Tony Gaddis and tackle them without reading the entirety of each chapter beforehand. I do plan on going back and doing the reading, but I really like trying something and stumbling through it with the current tools and knowledge that I have. Also, if you have any advice on things like syntax or if I’ve got something wrong, please don’t be afraid to let me know! I want to be a quality programmer!
The picture below is where I left off. And now that I’m editing this article, after completing these programs, I realized WHY this While loop wasn’t working: I wasn’t using the input function correctly! But now I have written this program in 2 different ways!
The issue here is that the loop runs through each iteration without pausing for input (I now know why). I did some more reading and decided I should try a For loop instead. You can see my For loop below but there’s of course an error because I reverted back to C++!
I got hung up here a bit because I thought that my For loop should work the way I wrote it. But I finally got the correct syntax and found this function called range to use in a for loop which is pretty cool. It looks more straightforward and easier to understand than the C++ syntax. I guess that’s why people love Python so much!
Time to run the code! My For loop is finally pausing to get user input (because I know how to use the input function properly)…but the total price is only displaying the last input and not adding up all 5 inputs…
That’s because my totalPrice variable is inside the For loop! Each time the loop iterates, the totalPrice variable gets reset back to 0 and whatever the last inputted number was gets added to that 0. I moved the totalPrice variable outside of the For loop. And I also fixed my formatting, as shown below.
Now that I’ve adjusted that, my program is displaying the correct total!
But we’re dealing with currency here, so I’ll round out to the 2nd decimal.
Now the loop is functioning as expected, I’ll add in the rest of the calculations and fix the formatting.
I want my printed output to say “Enter the price of item n:” where n is the counter. But as you can see in the error below, simply adding the counter variable in the output won’t work.
I did some googling and found the format function! Where curly brackets are, that’s where the variable will get appended to as shown below.
It’s looking better! I add the rest of the calculations, but there are still too many decimals:
I add the round function to the rest of the outputs. And here we have my finished program!
And just for fun, here’s my working program using a While loop without all of the fancy formatting and omitting the sales tax and total:
Distance Traveled: Here we need to write a program that calculates the distance a car going 60mph will travel in 5, 8, and 12 hours. After the previous program, I know that in a For (or any) loop I can specify how many iterations I want and the value of each iteration. See below:
Now that I see the calculations are correct and the For loop works as I’d like it to, I’ll add some context and formatting to the output. See below:
And our program is done!
Sum of Numbers: The goal of this program is to ask a user to enter a series of positive numbers. To end the program, the user must enter a negative number. The program then displays the sum of the positive numbers.
I love thinking through each challenge and deciding how I want my first version of my program to look like, as this is my starting point. As you can see, my first version is below.
The loop works but the program takes the negative number entered and subtracts it from the last positive number. I change a few things and come up with the 2nd version below, but it’s not escaping the loop even with negative numbers.
I messed around with the code some more and came up with the 3rd version below, and it works!
And there you have it, 3 programs completed! I’ve started working on a web scraper/crawler so maybe I’ll write about that next time.
If you have any tips or tricks you’d like to share with me, leave a comment or connect with me on LinkedIn and send me a message!