#๐Ÿ”’ Practice Q

200 messages ยท Page 1 of 1 (latest)

quartz epoch
#

Hi! We had this practice test and I was wondering what the missing blank could be.... here's the code I wrote
def count_members(file: TextIO) -> list[int]:
"""Given an open file containing group information as described above,
return a list containing the number of people in each group.
Every group begins with a GROUP # line (where # is the number for that group).
All member names contain only lower-case letters.
Precondition:
- the file has at least one group

file = open('sample_groups.txt')
>>> count_members(file)
[2, 3, 1]
"""
counts = []
line = file.readline().strip()
while line != '':
group_count = 0
line = file.readline().strip()
while line.islower() and 'GROUP' not in line:
counts.append(group_count)
return counts

I was wondering about the third final line for the conditions in the while loop. They asked us to fill out the two conditions between the and and I'm not sure if line.islower() is right.

proper quartzBOT
#

Hey @quartz epoch!

It looks like you are trying to paste code into this channel.

You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not """.

Here is an example of how it should look:
```
Hello, world!
```

This will result in the following:

Hello, world!```
You can **edit your original message** to correct your code block.
proper quartzBOT
#

@quartz epoch

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.

dire dragon
quartz epoch
#

I think one of them is right

#

idk about the other one

dire dragon
#

it might also be used to verify that the file isn't empty

#

so while line and 'GROUP' not in line

quartz epoch
#

while line != ''?

dire dragon
#

while line is basically the same as while line != ''

quartz epoch
#

how so

dire dragon
#

conditionally checking a datatype without a comparison checks for the "truthiness" of that datatype

#

any non-empty string is considered "truthy"

#

!e

name = 'bob'

if name:
    print("Hello")
proper quartzBOT
#

@dire dragon :white_check_mark: Your 3.12 eval job has completed with return code 0.

Hello
quartz epoch
#

oh shit

#

damn

dire dragon
#

yeah, python is cool

quartz epoch
#

so if it isn't empty

#

it'll check it

#

and say

#

this is a good string

dire dragon
#

yeah

#

all basic datatype have this

#

non-zero ints are truthy

#

non-empty lists are truthy

#

!e

items = []

if items:
    print("You have items!")
else:
    print("No items")
proper quartzBOT
#

@dire dragon :white_check_mark: Your 3.12 eval job has completed with return code 0.

No items
dire dragon
#

if items is the same as if len(items) > 0

quartz epoch
#

I see

#

that's pretty cool

#

I have a test today

#

I didn't study enough ๐Ÿ’€ so I'm doing it now

dire dragon
#

you'll also need to adjust the content of the while block

#

since you need to update line to the next line of the file

#

right now you're just appending

quartz epoch
#

did I not do line = readline()

#

file.readline()*

#

ohhhh

#

I didn't

#

in the second loop

#

I would probably do it after the counts

#

because I read line already

dire dragon
#

yeah, if you aren't changing line, then you'll create an infinite loop

quartz epoch
#

ok good point

#

oh I had another error

#

something with opening up a txt file to read it

#

do you mind if I shoud you

#

sgiw*

#

show*

dire dragon
#

go ahead ๐Ÿ™‚

quartz epoch
#
f = open('stuff.txt', 'r')
x = f.read()
for line in f: 
  print(line)

f.close()
f = open('stuff.txt', 'r')
print(f.readlines()[2].strip())```
#

it gives me this error

dire dragon
#

Where are you running this from?

quartz epoch
#

wing101 ide

dire dragon
#

can you comment out all the code and try writing this

quartz epoch
#

suer

dire dragon
#
import os

print(os.getcwd())
quartz epoch
#

wait I did that yesterday

#

you want me to comment the code out too

dire dragon
#

this will list your "working directory"

#

which is where it will look for files when your code runs

quartz epoch
#

hmm

#

and uncomment after @dire dragon ?

dire dragon
#

yes, this is just to test

quartz epoch
#

oko

#

ok I ran it

#

gonna uncomment now

#

still the wsame error

dire dragon
#

what was the output when you ran it...?

quartz epoch
#

same error

dire dragon
#

before you uncommented

#

it should have output a path

quartz epoch
#

ohhh

#

this

dire dragon
#

is that the folder you're working in?

quartz epoch
#

yea my downloads

dire dragon
#

that's where testfile.py is?

quartz epoch
#

yes\

dire dragon
#

and stuff.txt?

#

can you do this instead

#
import os

print(os.listdir())
#

sorry be back in a few min

quartz epoch
quartz epoch
dire dragon
#

your testfile doesn't show .py but your txt file does show .txt

quartz epoch
#

I wrote txt in iut

#

it

dire dragon
#

which is making me think you accidentally named your file stuff.txt.txt

#

do you see the stuff file when it listed the files?

quartz epoch
#

oh i do

#

it's txt

#

txt

dire dragon
#

๐Ÿ˜‰

#

I really recommend changing your windows settings to show file extensions

quartz epoch
#

is there a quick way to comment everything ๐Ÿ’€

#

I have to go thorugh all my code and uncomment and recomment

dire dragon
#

I'm not familiar with that ide but most have a hotkey

#

select everything and try ctrl+/

#

or ctrl+k

quartz epoch
#

ctrl / adds more comments

dire dragon
#

ctrl+shift+/ should be uncomment then

quartz epoch
#

ok yea

#

tytytytytyty

dire dragon
#

I also recommend not just working directly in your downloads folder

#

make a dedicated place on your pc where you work on your python files/projects

quartz epoch
#

why does this only print in happy ๐Ÿ’€

#

Like a folder?

dire dragon
quartz epoch
#

WHATTTTT

dire dragon
#

I'm not sure what you're asking

quartz epoch
#

uhh

#

I wasn't expecting this output

dire dragon
quartz epoch
#

here's the question

#

oh wait

#

read

#

sends it to the end

#

ughhhhhh I should know that

#

it's readline and readlines that do the other stuff

#

THEY T RICKED ME

dire dragon
#

welcome to programming ๐Ÿ™‚

#

You might have to actually think sometimes

quartz epoch
#

wait I wrote the right answer ๐Ÿ’€ Idk why I'm so surprised

#

yea programming is weird

#

I'm in a weird spot

#

I don't mind programming

#

but I'm not very good at it

#

and this isn't my future career path

dire dragon
#

no one is good at something they just started learning

quartz epoch
#

so like - idk where I'm going with this

dire dragon
#

it takes time and practice to get good at it

quartz epoch
dire dragon
#

I went to school for animation

#

now I code every day

quartz epoch
#

did you code during school

#

I find it hard sometimes

dire dragon
#

No, I'm entirely self-taught

#

tutorials, books, practice, asking questions

quartz epoch
#

you taught yourself after animation?

dire dragon
#

yes

#

3d animation is quite technical so you can use python to help automate some of the tedious parts

#

so I started coding to make my animation job easier, then decided I liked coding more than animating

quartz epoch
#

wow

#

that's amazing

dire dragon
#

and now I code automation tools for animators

quartz epoch
#

I'm trying to go into business

#

but things haven't been going my way recently ngl

#

every club I tried out for - I got rejected from

dire dragon
#

business is broad/vague

#

all of these skills need other skills to pair with it

#

you can't just be "business guy"

quartz epoch
#

it's my first year- so I think I wanna do something in finance

dire dragon
#

you have to be "sports business guy" or "restaurant business guy" or "gaming business guy"

quartz epoch
#

ah

dire dragon
#

it's the same with coding

#

hardly anyone successful is just "a coder"

quartz epoch
#

finance business guy ๐Ÿ˜Ž

dire dragon
#

I paired coding with animation

quartz epoch
#

I wanna see if I can use some coding with finance/trading

#

I guess I'll start working on it

dire dragon
#

yeah, that's a good combo

quartz epoch
#

I need more experience

dire dragon
#

I don't know anything about it though

quartz epoch
#

I can't be failing all the time ๐Ÿ’€

#

yea me neither but I will do my best fs

#

You've motivated me

dire dragon
#

for what it's worth, I went back to school for animation when I was 25

quartz epoch
#

yea I don't think like being a late starter is that bad

#

as long as you're learning yknow?

#

if you're making small steady progress

#

automatically makes you better than everyone who has 0 progress

dire dragon
#

I did school for digital design first and couldn't find work

quartz epoch
#

ahhh

#

switching lanes

dire dragon
#

slightly

#

I had one 3d animation class during that program

#

and decided that was what I fully wanted to follow

#

I did work as animator for nearly 4 years though before switching to what I do now

quartz epoch
#

damnnn

dire dragon
#

so yeah, you really never know where you might end up

quartz epoch
#

you're right

dire dragon
#

I think I'm finally where I want to be though

#

I love my job

quartz epoch
#

let's go

#

that's a win

#

if you like it

#

that's all that matters

proper quartzBOT
#
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.