#๐ Just need help with functions
76 messages ยท Page 1 of 1 (latest)
@grave kettle
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.
first off, don't use list as the variable name
what's that
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
Fifth, Matthew is in the list, but your function won't know who that is
sixth, there's no : on the if statement
I like how we're making a numbered list
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
!code format looks like this btw
print('Hello world!')
"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
referring back to the Second thing we mentioned, it looks like you might benefit with a dict. Could tackle that later.
seventh, there's no comma between list items "blue" and "Matthew"
what's a dict
!d dict
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)`
That's a dict โ๏ธ
e.g. in your case
person = [
{
"name": "Joe",
"age": 21,
"color": "blue",
},
]
would i repeat that with Matthew
well cos ur abbreviating it
wait do i have to do it like this
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
do i need the indent?
for readability, sure. Else, not really. You can put it all in one line if you want
ah ok
{"key":"value"}
I might even recommend not doing that.
Rather something like:
people = {
"Matthew": [18, "blue"],
}
ok i've fixed the dict issue
show us
Or
people = {
"Matthew": {
"age": 18,
"fav_color": "blue",
}
}
If i were using like 5 people, how would i like change the key, since everyone has an age
Well that inner dict is only assigned to Matthew
I prefer this. And then to access, say, the age, you would do
print(people["Matthew"]["age"]) # Prints 18
so i make like person1, person2 kinda thing?
no, just keep them all in one dict
so if i've done that, how do i make it so that it gets called in the list
people = {
"Matthew": {
"age": 18,
"fav_color": "blue",
},
"Bradley": {
"age": 24,
"fav_color": "chartreuse",
}
}
to check that it is Matthew age 21
!d dict.get to help check if the person and age exists
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).
how do i write that
!e
people = {
"Matthew": {
"age": 18,
"fav_color": "blue",
},
"Bradley": {
"age": 24,
"fav_color": "chartreuse",
}
}
print(people.get("Matthew")["age"])
@marsh urchin :white_check_mark: Your 3.12 eval job has completed with return code 0.
18
I just found out that you can .get .get multiple nested dicts
for fear of introducing complexity print(people.get("Matthew", {}).get("age") probably serves better
i'm so lost
Is this homework?
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
Do you just need to understand how to use functions in general or perform this specific task?
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
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