0

Below is a statement to be executed in the shell. Write Python statements to go before it that will guarantee that the statement will run without error. It doesn't matter what the code does, just that it runs. Below is an example. Code: a = p(b % c)

Statements to go before this one to guarantee that it will run without error:

def p(n):
    return n + 1
b = 45
c = 13

Code:

n = d[x](96) + 7

Statements to go before this one to guarantee that it will run without error:

def hello(n):
    return n + 5
d = {1:hello}
x = 1

I don't get the code. How can there be a square bracket, [x], with a parenthesis, (96), together to get a value in the dictionary d? What does that mean? Also, how come "hello" does not have quotes around the word since it is a string? I just don't get the code overall.

2
  • 3
    hello is a function not a string. 1 is the key in the dictionary which has a value of the function hello. Dictionary values can be anything and keys don't have to be strings. Commented Apr 23, 2012 at 5:21
  • I can't speak for others, but they may not like your question because it looks like you've copied it directly from your homework (if not, maybe you should be writing homework questions). The question might be a little easier to understand (for us) if you leave out the first part / add some explanation around the second part... Commented Apr 23, 2012 at 5:58

2 Answers 2

7

Since d is a dictionary, so accessing any element inside it requires you to use a index number, in the shown code, it is x. So, d[x] accesses the element at index x of dictionary.

When the defining of d is done, there is d = { 1:hello} this means that the 1th index of dictionary is referring to the predefined function named hello hence, during the call, there is a parenthesis used in the following line:

n = d[x](96) + 7

Since, we set x = 1, therefor, the call will actually be parsed as follows:

# d[x] calls dictionary element at index x
# x = 1, therefore, d[x] => d[1]
# d[1] is function hello
# d[1](96) will pass number 96 to function hello.
Sign up to request clarification or add additional context in comments.

Comments

2

a = p(b % c)

a is the name of the value returned by p. p is a function name ie def p(args): pass, (b % c) is the argument being fed to the p function, which finds the remainder from the division: b/c and passes that to p.

n = d[x](96) + 7

n is the name of the value returned by the value of the entry. d could be a list or a dict. [x] is the index of the item that is retrieved. (96) is the argument you're passing to the item returned from the function in d at index x. +7 is added to the object returned from the function d[x] with the argument 96.

Hopefully this will be a good starting point for you to understand what's going on.

2 Comments

Thank you very much. Your reply was helpful.
My pleasure. You seem to be asking a few questions here about your homework though. Part of python/coding is working out your own questions and just googling them. It's probably time for you to look up basic python tutorials, and just follow through them; it's easier than typing up questions for each assignment you've got due.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.