#๐Ÿ”’ Just need help with functions

76 messages ยท Page 1 of 1 (latest)

inner pendantBOT
#

@grave kettle

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.

marsh urchin
#

first off, don't use list as the variable name

violet heath
#

There's a number of issues here.

#

Second this list looks like it should be a dict

grave kettle
#

what's that

marsh urchin
#

Third, the identity function has a parameter fname but its not used in the function body

#

Fourth, use a code formatter available in Discord. Wrap your code in triple backticks followed by py after the first triple backticks

violet heath
#

Fifth, Matthew is in the list, but your function won't know who that is

daring niche
#

sixth, there's no : on the if statement

marsh urchin
#

lemon_xd I like how we're making a numbered list

grave kettle
#

mate im terrible at python

#

i was trying to make like a id list thing

#

where peoples names and age are stored, so if correct age and name + coloured entered they'd get greeted

marsh urchin
#

!code format looks like this btw

inner pendantBOT
#
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.

marsh urchin
#
print('Hello world!')
grave kettle
#
        "Matthew","73","red"]
def identity(name, age, colour):
    if name == 
        print("Hello", name, "you are", age, "years old")
        print("And your favourite colour is ",colour)
    else:
        print("Who are you?")


name = str(input("what is your first name?"))
age = int(input("What is your age"))
colour = str(input("What is your favourite colour"))

identity(name, age, colour)```.py
#

ok i just realised that the list shouldn't be called name

marsh urchin
daring niche
#

seventh, there's no comma between list items "blue" and "Matthew"

grave kettle
#

what's a dict

marsh urchin
#

!d dict

inner pendantBOT
#

class dict(**kwargs)``````py

class dict(mapping, **kwargs)``````py

class dict(iterable, **kwargs)```
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.

Dictionaries can be created by several means:

โ€ข Use a comma-separated list of `key: value` pairs within braces: `{'jack': 4098, 'sjoerd': 4127}` or `{4098: 'jack', 4127: 'sjoerd'}`

โ€ข Use a dict comprehension: `{}`, `{x: x ** 2 for x in range(10)}`

โ€ข Use the type constructor: `dict()`, `dict([('foo', 100), ('bar', 200)])`, `dict(foo=100, bar=200)`
marsh urchin
#

e.g. in your case

person = [
{
  "name": "Joe",
  "age": 21,
  "color": "blue",
},
]
grave kettle
#

would i repeat that with Matthew

marsh urchin
#

pretty much yeah

#

but put it in as a list of dicts

#

(sounds weird saying out loud)

grave kettle
#

well cos ur abbreviating it

grave kettle
marsh urchin
#

Its an example

#

But it looks like that

#
{
  "key" : "value"
}
#

It has a key and if you access that dict's key, you get the value

grave kettle
#

do i need the indent?

marsh urchin
grave kettle
#

ah ok

marsh urchin
#
{"key":"value"}
violet heath
grave kettle
#

ok i've fixed the dict issue

marsh urchin
grave kettle
#
  "Joe": [21, "red"]
  "Matthew": [18, "blue"],
}```
#

i might be missing commas

violet heath
#

Or

people = {
    "Matthew": {
        "age": 18,
        "fav_color": "blue",
    }
}
grave kettle
violet heath
marsh urchin
grave kettle
#

so i make like person1, person2 kinda thing?

marsh urchin
#

no, just keep them all in one dict

grave kettle
#

so if i've done that, how do i make it so that it gets called in the list

violet heath
#
people = {
    "Matthew": {
        "age": 18,
        "fav_color": "blue",
    },
    "Bradley": {
        "age": 24,
        "fav_color": "chartreuse",
    }
}
grave kettle
#

to check that it is Matthew age 21

marsh urchin
#

!d dict.get to help check if the person and age exists

inner pendantBOT
#

get(key[, default])```
Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to `None`, so that this method never raises a [`KeyError`](https://docs.python.org/3/library/exceptions.html#KeyError).
grave kettle
#

how do i write that

marsh urchin
#

!e

people = {
    "Matthew": {
        "age": 18,
        "fav_color": "blue",
    },
    "Bradley": {
        "age": 24,
        "fav_color": "chartreuse",
    }
}
print(people.get("Matthew")["age"])
inner pendantBOT
#

@marsh urchin :white_check_mark: Your 3.12 eval job has completed with return code 0.

18
marsh urchin
#

I just found out that you can .get .get multiple nested dicts

rare lion
#

for fear of introducing complexity print(people.get("Matthew", {}).get("age") probably serves better

grave kettle
#

i'm so lost

smoky pulsar
#

Is this homework?

grave kettle
#

no i'm attempting to revise for a test

#

and i need to learn how to do subprograms

#

I think i'm just ganna close this

smoky pulsar
#

Do you just need to understand how to use functions in general or perform this specific task?

grave kettle
#

well there's a practise question

#

and i think it uses functions?

smoky pulsar
#

Maybe you should start with a simpler function. Maybe familiarize yourself with different built-in Python data types too (str, list, set, dict, tuple, etc)

#

otherwise you're going to end up feeling overwhelmed

grave kettle
#

alright

#

i'm ganna just delete the post

#

and just do simpler stuff

inner pendantBOT
#
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.

#

๐Ÿ”’ Just need help with functions