I would like to get some help to understand the following code for "Higher or Lower game reproduction from one of the course I am doing right know.
For the second line of code where the function is defined as : account_name=account["name"]
I do not understand why they named as "account" where they referring to the key value "name"
. When I learned functions, I thought the name where it says "account" has to say "data", because that is the name of the list that we going to use to get the info. Whilst we going to define a new variable like account_name=data["name"]
. But looks like I am wrong. The other thing is why we passing a value to the function "format data" which is nowhere In the code as "account".
So would like to know how the function gets all the necessary information when the list name is completely different, how it is getting the value.
Every advice is appreciated.
from game_data import data
#function that converts the data to a certain format to display
def format_data(account):
account_name=account["name"]
account_follower=account["follower_count"]
account_desc=account["description"]
account_country=account["country"]
return f"{account_name} {account_follower} {account_desc} {account_country}"
#randomly chooses one of the dictionaries from the list
account_a=random.choice(data)
account_b=random.choice(data)
if account_a==account_b:
account_b=random.choice(data)
print(f"compare A: {format_data(account_a)}")
print(f"compare B: {format_data(account_b)}")
CodePudding user response:
My guess is that given the line: from game_data import data
this makes the variable data
available, which is a list
.
The line account_a=random.choice(data)
, then randomly picks one of the items.
The line: print(f"compare A: {format_data(account_a)}")
then calls the format_data()
function sending it the parameter account_a
.
Now the format_data()
function runs for the first time with parameter account
which refers to the same object that account_a
refers to.
All this function does it to pick out some elements of the dict
and reformats them into a string which is returned.
This formatted string then gets printed by the above: print(f"compare A: {format_data(account_a)}")
The thing you get confused about is calling functions and the names of parameters.
Where there is the call: format_data(account_a)
, python simply does an assignment of:
account = account_a
as part of the call and the function format_data()
now has the parameter account
.
CodePudding user response:
In the example you have given, the line:
def format_data(account):
Tells us that there is a method which takes a parameter, which in this instance is called "account". The parameter could be named anything at all (with some exceptions), such as "variable", "foo" or "x". However it is useful to give it a meaningful name, and in this case they have gone for "account". This name "account" is used throughout the function and it acts on whatever has been passed in by whatever called the function in the first place.
It is worth noting that the variable "account" is only in scope for that function. If you tried to access "account" outside of the function you would get an error. This is where the idea of variable scope comes in, and you can read about this more if you search online.
If you chose to use the variable name "data" for the function it would be possible, but actually could be more confusing as then there would be two variables, both called "data" but with different scopes. One is in scope for the function call, and one for the script. So it is best to not use variable names in that way, and instead choose different names to avoid confusion and accidental mistakes.