#πŸ”’ While and For Loops HW Help

84 messages Β· Page 1 of 1 (latest)

unkempt rover
#

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!

zinc cypressBOT
#

@unkempt rover

Python help channel opened

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.

orchid bone
unkempt rover
#

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:
orchid bone
#

!code

zinc cypressBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

unkempt rover
#

Oh shoot I forgot comments do that here husk

orchid bone
#

can you wrap that in a codeblock?

unkempt rover
#

Sure I can do that

twilit canyon
#

Use a dict. Looking at the images a dict will do. Take input for each key and update the respective value.

orchid bone
#

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

unkempt rover
#

Well it was more like the assignment is asking to not even engage the while loop if the sentinel value is immediately entered

unkempt rover
orchid bone
#

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?

unkempt rover
#

Yeah I have that

#

But not in code anywhere

orchid bone
#

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?

unkempt rover
#

The assignment wants them in ounces

unkempt rover
#

I don't know if my teacher wants us to use objects or not

orchid bone
#

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)

orchid bone
twilit canyon
#

Can you explain what the program does?

unkempt rover
#

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?

orchid bone
unkempt rover
#

But then you assign data to the corresponding metadata for the object and it creates like a new object each time

orchid bone
#

"object(s)" can mean a variety of things in python depending on the context

unkempt rover
orchid bone
# unkempt rover 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

unkempt rover
#

I don't, no

#

Other than her just reiterating to not use breaks

orchid bone
#

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

unkempt rover
#

So is that something I do through the list I have now or is there another way I should be doing that?

orchid bone
#

Do you know how to use dicts yet?

unkempt rover
#

No clue

orchid bone
#

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

unkempt rover
#

We've pretty much only covered input/output, if statements, and loops so far

twilit canyon
#

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.

unkempt rover
#

I don't know what that entails

#

Or what that is

orchid bone
unkempt rover
#

I'm not even really sure how the list works honestly

#

I haven't seen anything in the textbook for it so far

orchid bone
#

!e

my_list = [0, 3, 10]
print(my_list[0])
my_list[1] += 1
print(my_list)
zinc cypressBOT
#

@orchid bone :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0
002 | [0, 4, 10]
orchid bone
#

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

unkempt rover
unkempt rover
# unkempt rover

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?

orchid bone
#

!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)
zinc cypressBOT
#

@orchid bone :white_check_mark: Your 3.12 eval job has completed with return code 0.

[2, 1, 1]
unkempt rover
#

Okay

orchid bone
#

hopefully the example makes it clear

#

||for those watching, I purposely didn't index item||

unkempt rover
#

I need to get that to look like this in the output somehow though

Jumbo: **
XL: *
Large: ***
Medium: ****
Small: ***
#

Something like that

orchid bone
#

Yeah, once you're able to get the category counts correct, then you can work on printing that display

unkempt rover
#

So do I have to compare it to the list somehow?

#

To get that display?

orchid bone
#

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)
zinc cypressBOT
#

@orchid bone :white_check_mark: Your 3.12 eval job has completed with return code 0.

!!!!
unkempt rover
#

Is it possible to read the list and print it that way?

orchid bone
#

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)

unkempt rover
#

Like you directly call the list and print it

twilit canyon
#

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

unkempt rover
#

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

orchid bone
#

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

unkempt rover
#

Alright

#

Thanks for the help so far

#

If I just have something that runs I guess that'll be the first milestone for me πŸ˜…

unkempt rover
#

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

zinc cypressBOT
#
Python help channel closed

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.