So for a homework assignment I have to write code to essentially print out this using for loops based on inputs from a while loop, and I'm not quite sure how to go about it. My current idea is to use a list of some kind but I'm not sure how to actually increase the values in the list based on the different weight categories. Any help with that would be much appreciated, thank you!
#π While and For Loops HW Help
84 messages Β· Page 1 of 1 (latest)
@unkempt rover
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
What would the inputs look like? Just someone entering the size of eggs until they say stop?
Yeah entering the weights of the eggs in ounces until they enter the sentinel value to stop the loop
This is currently what my code is:
# Setting initial variables
total_eggs = 0
total_weight = 0
weight_max = ''
weight_min = ''
weight_cat_count = [0, 0, 0, 0, 0]
# Taking input float of egg weight
egg_weight = float(input('Enter the egg weight (0 to quit): '))
# While loop to get input of egg weights
while egg_weight != 0:
total_eggs += 1
total_weight += egg_weight
if egg_weight > weight_max:
weight_max = egg_weight
if egg_weight < weight_min:
weight_min = egg_weight
# Restarting the loop via input
egg_weight = float(input('Enter the egg weight (0 to quit): '))
# Printing total eggs that increment with each iteration of the loop
print(f'Number of eggs: {total_eggs:,.2f}')
# Calculating and printing average egg weight
avg_weight = total_weight / total_eggs
print(f'Average egg weight: {avg_weight:.2f}')
# Printing maximum egg weight
print(f'Heaviest egg weight: {weight_max:,.2f}')
# Printing minimum egg weight
print(f'Lightest egg weight: {weight_min:,.2f}')
for egg_weight in range(1, total_eggs +1):
if egg_weight > 2.5:
!code
Oh shoot I forgot comments do that here 
can you wrap that in a codeblock?
Sure I can do that
Use a dict. Looking at the images a dict will do. Take input for each key and update the respective value.
So what I would focus on next is coding the loop you need that asks the user for input until they enter a stop value
Once you have the input from the user, it'll be a bit easier to figure out how to sort
Well it was more like the assignment is asking to not even engage the while loop if the sentinel value is immediately entered
Is that not just this?
while egg_weight != 0:
total_eggs += 1
total_weight += egg_weight
if egg_weight > weight_max:
weight_max = egg_weight
if egg_weight < weight_min:
weight_min = egg_weight
# Restarting the loop via input
egg_weight = float(input('Enter the egg weight (0 to quit): '))
Well, you do have the secondary while loop asking for input, but you're not quite capturing the input for later.
So I guess the question is, how do you know what size an egg is based on the weight? Do you have that breakdown?
Okay cool, so you can use that to count which egg sizes are being input. That's what the egg_cat_count variable is for, right?
The assignment wants them in ounces
Yeah
I don't know if my teacher wants us to use objects or not
So, once you get the egg weight, add in some code to figure out what size it is. Then once you know the size, increment the correct count in the egg_cat_count list (it's also worth looking at using a dict there, if you know how to use dicts)
like a custom class or do you mean something else by object?
Can you explain what the program does?
I'm not 100% sure honestly, I'm still confused as to what objects like are in the code itself
My dad, who has background in coding, was just talking to me about them and like from my understanding basically the object itself gets defined with metadata?
Do the instructions mention "objects" at all?
But then you assign data to the corresponding metadata for the object and it creates like a new object each time
"object(s)" can mean a variety of things in python depending on the context
Not that I saw?
So I wouldn't worry about it too much then, unless you have some specific instructions from your teacher that are outside the instructions
So I'd focus on adding in some code that can categorize each egg the user enters into the correct category. Then you can work on adjusting the correct category total from there
So is that something I do through the list I have now or is there another way I should be doing that?
Do you know how to use dicts yet?
No clue
So just use the list you have for the time being to get something working
Once you have something working I can show you how using a dict might be an improvement
We've pretty much only covered input/output, if statements, and loops so far
Don't make the program to complicated. Use a dictionary update the dictionary as necessary. Sort the dictionary if needed. Take the input in while loop with a breaking condition. Print out the updated dict with a function call or just write print statement in your code.
a dictionary isn't required here, you can use the list for the time being
Okay, so how do I like increase the individual values for the list?
I'm not even really sure how the list works honestly
I haven't seen anything in the textbook for it so far
!e
my_list = [0, 3, 10]
print(my_list[0])
my_list[1] += 1
print(my_list)
@orchid bone :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0
002 | [0, 4, 10]
So a list is a dynamic way to hold a variable amount of things. You can see there that I can change individual values inside a list if I want.
Python generally uses 0-based indexing, so the first item in the list has an index of 0. The second item has an index of 1, so on and so forth.
So, based on the egg size, you can increment the number for the item in the list that represents that egg category
Oh so does that just do it to the 2nd value?
So if I have 0 is Jumbo, 1 is Extra Large, 2 is Large, 3 is Medium, and 4 is Small I just replace the 1 in my_list[1] with whatever position I want to increase?
!e
things_to_sort = ["apple", "banana", "cherry", "apricot"]
alphabet_fruit_count = [0, 0, 0]
for item in things_to_sort:
if item.startswith("a"):
alphabet_fruit_count[0] += 1
elif item.startswith("b"):
alphabet_fruit_count[1] += 1
elif item.startswith("c"):
alphabet_fruit_count[2] += 1
print(alphabet_fruit_count)
@orchid bone :white_check_mark: Your 3.12 eval job has completed with return code 0.
[2, 1, 1]
Okay
hopefully the example makes it clear
||for those watching, I purposely didn't index item||
I need to get that to look like this in the output somehow though
Jumbo: **
XL: *
Large: ***
Medium: ****
Small: ***
Something like that
Yeah, once you're able to get the category counts correct, then you can work on printing that display
Start with just figuring out how to print this:
Jumbo: 2
XL: 1
Large: 3
Medium: 4
Small: 3
just do the counts first. You already know which egg size has which counts so one or more print statements should do the trick
One fun thing with Python that may be helpful here is what happens when you multiply a string
!e
my_str = "!"
print(my_str*4)
@orchid bone :white_check_mark: Your 3.12 eval job has completed with return code 0.
!!!!
Is it possible to read the list and print it that way?
in what way do you mean? (this is part where using a dict might be more helpful, but if you haven't learned them yet then using a list will require just a tad bit more code)
Like you directly call the list and print it
Use two list to list one to know the position of the value list and one for storing the value.
keypoint = ['jumbo']
values = [0]
values[keypoint.index('jumbo')] = new valeu
I don't think lists were even in the textbook honestly it was just something I found with searching around
At least in anything I read in the textbook so far that I can remember
I need to drop for the evening, but try just using basic print statements for the time being and then you can see how to optimize it using the method that Your name is suggesting or a dict
Alright
Thanks for the help so far
If I just have something that runs I guess that'll be the first milestone for me π
This is going so great /sar
Enter the egg weight (0 to quit): 2.6
Enter the egg weight (0 to quit): 2.4
Traceback (most recent call last):
line 27, in <module>
elif egg_weight < weight_min:
TypeError: '<' not supported between instances of 'float' and 'NoneType'
# Setting initial variables
total_eggs = 0
total_weight = 0
weight_max = 0
weight_min = None
# Setting lists for the size categories and their frequencies
# weight_cat = [Jumbo, Extra_Large, Large, Medium, Small]
weight_cat_count = [0, 0, 0, 0, 0]
# Taking input float of egg weight
egg_weight = float(input('Enter the egg weight (0 to quit): '))
# While loop to get input of egg weights
while egg_weight != 0:
total_eggs += 1
total_weight += egg_weight
if egg_weight > weight_max:
weight_max = egg_weight
elif egg_weight is None:
weight_min = egg_weight
elif egg_weight < weight_min:
weight_min = egg_weight
# Restarting the loop via input
egg_weight = float(input('Enter the egg weight (0 to quit): '))
It's 11:30 I should probably just call it good for the night
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.