#๐Ÿ”’ Can anyone help me with homework?

211 messages ยท Page 1 of 1 (latest)

wraith void
#
  1. Find the most frequent appearing value in a dictionary, if there are ties, just return any one among them
    eg.
    input: {"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}
    output: 4.0

  2. Given a list of numbers, return a list with the order of numbers reversed (challenging)
    hint: try to use the range function in a forloop, by counting the numbers in the list in reverse
    then try to build a new list out of it
    input: [1,2,3,4,5]
    out: [5,4,3,2,1]

subtle schoonerBOT
#

@wraith void

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.

wraith void
#

how do i write a list?

#

i know .dict doesnt allow duplicates

#

tht i know

static ravine
#
list = [1, 2, 3]
wraith void
#

how can i write a list using input?

#

input("Write a list of numbers")

#

how do i get python to detect tht list of numbers?

#

anyone here?

static ravine
#

Uhh

fiery depot
#

What do they mean by "given a list of numbers"? That can mean different things.

wraith void
#

urm

#

like

#

input("1, 2, 3, 4, 5")

#

and then make python write

#

5 4 3 2 1

#

yea

static ravine
#

I don't think they're looking for an input

#

I think it's just in the code

wraith void
#

mind getting the 2nd one done first?

#

i know what .dict is, HOW DO I WRITE A LIST WITH .DICT

#

LIKE

fiery depot
#

If you mean a comma separated string representing a list, look j into the split method on strings.

wraith void
#

list.dict?

#

list.dict = [1, 2, 3, 4, 5, 5] i have tried this

#

gives me an error

#

TypeError: cannot set 'dict' attribute of immutable type 'list'

#

wtf is tht supposed to mean

static ravine
#

I don't think you're supposed to use list.dict

fiery depot
#

That line of code attempts to create a dict attribute on the list class, because you did an assignment.

#

What are you trying to do there?

wraith void
#

.dict doesnt allow duplicats correct?

#

im tryna like get .dict to give me the duplicate

fiery depot
wraith void
#

wdym?

#

like 1, 1, 2, 2, 3,

#

would be 1, 2, 3?

fiery depot
#

A dictionary can't have two of the same key:

#

!e

d = { 1: 'a', 1: 'b', 2: 'c', 3: 'd' }
print(d)
subtle schoonerBOT
fiery depot
#

Notice there's only a single 1 key.

wraith void
#

thought it was number?

fiery depot
#

You thought what was a number?

wraith void
#

I thought .dict removed duplicate numbers

#

like 1, 1, 2, 2, 3, 3, it would be 1, 2, 3, after

fiery depot
fiery depot
#

My code shows that d was created with two 1 keys, but only one remained after the dictionary was created.

wraith void
#

oh

#

ohh

wraith void
fiery depot
#

!e

d = { 1: 'a' }
print(d, type(d))
subtle schoonerBOT
fiery depot
#

How have you been creating dictionaries?

wraith void
#

uh

#

here is the thing

#

i never did-

#

d = { "john": '3.5', "jane": '4.0', "bob": '2.8', "mary": '4.0' }
print(d)

#

thissss

#

doesnt work

#

i tried replacing everything

#

AUGHHH

fiery depot
#

That code looks like it will execute fine

#

!e

d = { "john": '3.5', "jane": '4.0', "bob": '2.8', "mary": '4.0' }
print(d)
subtle schoonerBOT
fiery depot
#

It created a dictionary and stored it in d.

wraith void
#

how do i get it

#

to output the 4.0

fiery depot
#

You'd need to determine which value is occurring the most frequently.

wraith void
#

tht would be 4.0

fiery depot
#

Which means you want to check every value in the dictionary.

wraith void
#

and how do i do tht?

fiery depot
wraith void
#

(im a total beginer)

fiery depot
#

!e

d = { 1: 'a', 2: 'b', 3: 'c' }
for value in d.values():
    print(value)
subtle schoonerBOT
fiery depot
#

Reverted to Javascript for a second there lmao

wraith void
#

have not touched tht yet

fiery depot
#

The part after the : in the dictionary

wraith void
#

brb

fiery depot
#

You're likely going to need to take a step back and study dictionaries. You won't be able to use them for things like this without having a good undertanding of them.

static ravine
#

Dictionary are lists where you can access a item in the list with a key instead of a number.

#

for example

#

{"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}

fiery depot
#

Well, to be clear, they're not lists. They're comparable to lists in the sense that they can contain other things, and allow looking up specific elements by some key/index.

static ravine
#

^

static ravine
wraith void
#

erm idfk what a value is

#

here is the thing

#

if i havent learned it

#

i cant use it

static ravine
#

yeah im trying to help you learn it

fiery depot
#

You can't use a dictionary without using "values" of the dictionary.

#

If they are requiring you to use a dictionary, they're expecting you to understand values.

wraith void
#

oop

#

tell tht to the github website my teach is using

#

if u click on the .dict py one

#

there is no values

static ravine
#

!e

d = {"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}

print(d["john"])
subtle schoonerBOT
subtle schoonerBOT
#

ultimatepython/data_structures/dict.py line 14

# Let's create a dictionary with student keys and GPA values```
wraith void
#

d = { "john": '3.5', "jane": '4.0', "bob": '2.8', "mary": '4.0' }
print(d)
for value in d.values():
print(value)

subtle schoonerBOT
#

Hey @wraith void!

Please edit your message to use a code block

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

This will result in the following:

print('Hello, world!')```
wraith void
#

IT DOESNT DELETE THE STUPID 4.0

#

AUGH IM GONNA CRASH OTU

static ravine
wraith void
#

wdym

#

key?

#

gives the wrong number

#

wait now it doesnt

#

i changed it to mary

#

alr now

#

can anyone help w/ the 3.

#

Given a list of numbers, return a list with the order of numbers reversed (challenging)
hint: try to use the range function in a forloop, by counting the numbers in the list in reverse
then try to build a new list out of it
input: [1,2,3,4,5]
out: [5,4,3,2,1]

static ravine
#

!e

inputlist = [1,2,3,4,5]
newlist = [
for i in list[::-1]
  newlist.append(i)
#

!e

inputlist = [1,2,3,4,5]
newlist = []
for i in list[::-1]
  newlist.append(i)
#

bruh

wraith void
#

tf

#

help

static ravine
#

!e

inputlist = [1,2,3,4,5]
newlist = []
for i in list[::-1]:
  newlist.append(i)
wraith void
#

help

static ravine
#

trying

wraith void
#

someone also gotta explain it

#

btw

static ravine
#

!e

inputlist = [1,2,3,4,5]
newlist = []
for i in list[::-1]:
  newlist.append(i)
print(newlist)
wraith void
#

i aint looking for answers, im looking for explanations

static ravine
#

yeah ill explain

#

one moment

spiral fulcrum
#

They asked to use range though

static ravine
#

Ok so one way to do it would be this:

inputlist = [1,2,3,4,5]
newlist = []

for i in reversed(inputlist):
  newlist.append(i)
print(newlist)

It iterates over the list reversed(inputlist) with a for loop. reversed() is a function that outputs a reversed version of a list, inside of the for loop it appends the current number to an empty list.

static ravine
wraith void
#

yea

#

i was readin the code

#

and was like

#

where did the range code

#

*go

spiral fulcrum
wraith void
#

not at all

#

im quite the forgetful one

spiral fulcrum
#

range is basically a shortcut for making a sequence of numbers

#

range(5) is roughly the same as [0, 1, 2, 3, 4]

wraith void
#

ohh

#

alr now

#

Given a list of numbers, return a list with the order of numbers reversed (challenging)
hint: try to use the range function in a forloop, by counting the numbers in the list in reverse
then try to build a new list out of it
input: [1,2,3,4,5]
out: [5,4,3,2,1]

help/

spiral fulcrum
#

do you understand what indexing is?

wraith void
#

nope

spiral fulcrum
#

Ok, I'm going to use an example but with letters instead

#

because I think the numbers actually make this more confusing

#
letters = ['a', 'b', 'c', 'd', 'e']
#

this is a list of letters

#

"indexing" lets us access something at a specific position in the list

#

so if I just wanted "c"

#

I would do letters[2]

wraith void
#

mhm

spiral fulcrum
#

we start counting from 0 in indexing

#

so "a" is 0

#

"b" is 1

#

etc

wraith void
#

ohh

#

alr alr i see

spiral fulcrum
#

now there's some useful relationships to keep in mind

wraith void
#

wait

#

whats reversed in python

#

i js typed it in

spiral fulcrum
#

the last position in the list will always be "the length of the list minus one"

wraith void
#

and it turned blue

spiral fulcrum
#

it's a function

#

(technically it's a class but I won't get into that now)

wraith void
#

tf does it do

spiral fulcrum
#

it reverses things

wraith void
#

its giving me exit code 0

#

WAHAT

#

IHQEIOQWE

#

WAIT

#

WAIT

#

WHAT NOW

#

I NEED THIS THING

spiral fulcrum
#

there's definitely easier ways to reverse things than using range

#

but they asked you to use range

#

learning programming isn't about just finding the easiest way to do things

#

it's about learning how to think logically

wraith void
#

me being excited for liek 1 second

wraith void
#

yep

spiral fulcrum
#

ok

#

so something nice about range, is if we give it a range based on the length of a list, we get the correct index count

#

!e

letters = ['a', 'b', 'c', 'd', 'e']

for i in range(len(letters)):
    print(letters[i])
subtle schoonerBOT
spiral fulcrum
#

Does this make sense to you?

wraith void
#

yo wait

#

i got it workin

#

it js uh

#

has a lot of commas in it

#

result = reversed("1, 2, 3")
print(list(result))

spiral fulcrum
#

don't use reversed here

#

if you'd like me to stop explaining, I can

wraith void
#

wait i gtg sleep now

#

augh

#

ill read it later

#

!close

subtle schoonerBOT
#
Python help channel closed with !close

This help channel has been closed. 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.