Executing code, which statements are true?

AI Thread Summary
The discussion revolves around two Python code snippets and the evaluation of statements regarding variable scope and values after execution. In the first question, the key points include that two variables named "degrees" exist in different namespaces, and the value of "degrees" in the function namespace is 10. For the second question, it is clarified that the function does not return a value, and the output is printed directly, confirming that the correct answer is that an output is generated. Participants emphasize the distinction between local and global variables, noting that the function can access the global "degrees" variable when no parameters are defined. Overall, the conversation highlights the importance of understanding variable scope in Python.
hnnhcmmngs
Messages
19
Reaction score
0

Homework Statement



Question 1:
Python:
def convert(degrees):
    degrees += 273.15
    return degrees

degrees = 10
print(convert(degrees))

Which of these statements are true after the code executes? (There may be more than 1 correct answer)
a) The value of degrees in the main namespace is 283.15
b) Two variables called degrees have been created
c) The value of degrees in the function namespace is 10
d) This code generates an error because you cannot use the same variable name twice.

Question 2:
Python:
def convert():
    print('After conversion, there are', degrees + 273.15, 'degrees')

degrees = 10
print(convert())

Which of these statements are true after the code executes? (There may be more than 1 correct answer)
a) The return value of the function is 283.15
b) This code generates an error.
c) A line of output is printed from inside the function.
d) Two variables called degrees have been created
e) The value of degrees in the main namespace is 283.15

Homework Equations


[/B]
N/A

The Attempt at a Solution


[/B]
For Question 1, I think b) is one of the correct solutions and I know that d) can't be right because I ran the program and it didn't generate an error.

For Question 2, a) should be one of the correct answers and b) can't be right because the code doesn't generate an error.
 
Last edited by a moderator:
Physics news on Phys.org
Which computer language is this for?
 
berkeman said:
Which computer language is this for?
Oh I'm sorry I should have included that! It's Python.
 
Okay, thanks. I went ahead and added code tags to your code snippets and specified Python. It's usually best to post code with code tags.

I'm just learning Python right now, so I'm not qualified to help you with this. Others will be able to chime in. :smile:
 
  • Like
Likes StoneTemplePython
For Question 1, why don't you just add a line after the last print statement to see what the value of degrees is after execution, like this:
Python:
def convert(degrees):
    degrees += 273.15
    return degrees

degrees = 10
print(convert(degrees))

print(degrees)

For Question 2, you need to think harder. a) is not the correct answer. Can you tell me why?
 
phyzguy said:
For Question 1, why don't you just add a line after the last print statement to see what the value of degrees is after execution, like this:
Python:
def convert(degrees):
    degrees += 273.15
    return degrees

degrees = 10
print(convert(degrees))

print(degrees)
So for Question 1 I guess the only correct answer would be c)?

phyzguy said:
For Question 2, you need to think harder. a) is not the correct answer. Can you tell me why?
And a) wouldn't be correct since the return value is "After conversion, there are 283.15 degrees", correct?
So for Question 2 the only correct answer is c)?
 
hnnhcmmngs said:
So for Question 1 I guess the only correct answer would be c)?And a) wouldn't be correct since the return value is "After conversion, there are 283.15 degrees", correct?
So for Question 2 the only correct answer is c)?

I think you are correct in that these are correct answers for the two problems. The question is whether these are the only correct answers. Note that in Question 2, the function doesn't return anything, so a) can't be right. What about b) in Question 1? Isn't the variable degrees in the main namespace a different variable from the variable degrees in the function?
 
phyzguy said:
I think you are correct in that these are correct answers for the two problems. The question is whether these are the only correct answers. Note that in Question 2, the function doesn't return anything, so a) can't be right. What about b) in Question 1? Isn't the variable degrees in the main namespace a different variable from the variable degrees in the function?
I think I'm good on Question 2, and c) is the only right answer.

For Question 1, a) isn't right and d) also isn't right. I guess my problem with Question 1 is that I don't know what the main namespace refers to and what the function namespace refers to.
 
hnnhcmmngs said:
For Question 1, a) isn't right and d) also isn't right. I guess my problem with Question 1 is that I don't know what the main namespace refers to and what the function namespace refers to.
The function namespace are the statements within the function. The main namespace are the statements outside of the function. If you print out the value of the variable "degrees" in those two namespaces, you will see it has a different value in the two namespaces. While it has the same name in the two namespaces, it is really two different variables stored in two different locations in computer memory.
 
  • Like
Likes Klystron, hnnhcmmngs and scottdave
  • #10
berkeman said:
Okay, thanks. I went ahead and added code tags to your code snippets and specified Python. It's usually best to post code with code tags.

I'm just learning Python right now, so I'm not qualified to help you with this. Others will be able to chime in. :smile:
Hey, I figured how to do the CODE tags, but how do you get it to say Python (or other language)?
 
  • #11
scottdave said:
Hey, I figured how to do the CODE tags, but how do you get it to say Python (or other language)?
[ code=Python ] lakj;flkj;alkdj;lkjd; [ /code ]

(without the spaces) :smile:
 
  • Like
Likes scottdave
  • #12
phyzguy said:
I think you are correct in that these are correct answers for the two problems. The question is whether these are the only correct answers. Note that in Question 2, the function doesn't return anything, so a) can't be right. What about b) in Question 1? Isn't the variable degrees in the main namespace a different variable from the variable degrees in the function?
When you use the +=, the variable has to already exist, or there will be an error.
If we type
Python:
 x = x + 1
without first defining x, then we get an error because x does not have a value yet to add 1 to. This is the same with x += 1.
 
  • #13
scottdave said:
When you use the +=, the variable has to already exist, or there will be an error.
I'm not sure what your point is here. There is no doubt that the variable "degrees" exists within the function. The point is that the variable named "degrees" within the function is a different variable from the variable named "degrees" in the main namespace. Do you disagree?
 
  • #14
scottdave said:
Hey, I figured how to do the CODE tags, but how do you get it to say Python (or other language)?
Also see the stickied threat at the top of the threads in this section -- https://www.physicsforums.com/threa...-for-nearly-200-programming-languages.774303/

scottdave said:
When you use the +=, the variable has to already exist, or there will be an error.
Yeah, I don't see what you point is, either. In the problem shown in this thread --
Python:
def convert(degrees):
    degrees += 273.15
    return degrees
-- the parameter named degrees is defined in the function definition.
 
  • #15
Mark44 said:
Also see the stickied threat at the top of the threads in this section -- https://www.physicsforums.com/threa...-for-nearly-200-programming-languages.774303/

Yeah, I don't see what you point is, either. In the problem shown in this thread --
Python:
def convert(degrees):
    degrees += 273.15
    return degrees
-- the parameter named degrees is defined in the function definition.
Well I was referring to his question 2, where no variables are defined inside the function. I tried running this. I was expecting an error.

In Python, variable created inside of a function are not normally available outside of the function. Functions do not typically have access or can modify global variables unless they are passed as arguments. (So I thought) Lists and dictionaries are exceptions I believe.

In this example, apparently specifying
def convert():
with no arguments, allows the function to access the global variable degrees.
 
Last edited:
  • #16
Well I learned something today. :wink:
 
  • #17
scottdave said:
In this example, apparently specifying
def convert():
with no arguments, allows the function to acxess the global variable degrees.
Yes, I believe that's the case.
From the Python docs (v 3.4.2):
When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s environment.
The function body is a block and the entire script is a block that encompasses the function code block. Since degrees is merely used in the function block but not defined there, the interpreter resolves things by look at the neares enclosing scope, the scope of the entire script.
 
  • Like
Likes berkeman and scottdave
  • #18
That's why in Question 2, d) Two variables called degrees have been created is not correct, but in Question 1, b) Two variables called degrees have been created, is correct.
 
  • #19
phyzguy said:
but in Question 1, b) Two variables called degrees have been created, is correct.
I'm not certain about this answer. The local variable degrees has been created, but at the end of the function it is also destroyed again, so it doesn't exist anymore at the end of the program.
 
  • #20
Someone remember how to get the listing of nearly 200 languages ?
 
  • #22
willem2 said:
I'm not certain about this answer. The local variable degrees has been created, but at the end of the function it is also destroyed again, so it doesn't exist anymore at the end of the program.
Well, we're getting into semantics here, but even though the local variable has been destroyed, I think it is still true that that two variables have been created.
 
  • Like
Likes scottdave
  • #23
willem2 said:
I'm not certain about this answer. The local variable degrees has been created, but at the end of the function it is also destroyed again, so it doesn't exist anymore at the end of the program.
But there were 2 different memory references. If you have degrees outside the function, then create a degrees variable inside, it does not affect the outside variable.
 
  • #24
scottdave said:
But there were 2 different memory references. If you have degrees outside the function, then create a degrees variable inside, it does not affect the outside variable.

Of course that's true. Is it relevant to the question? Are you saying that two variables called degrees have not been created?
 

Similar threads

Replies
2
Views
2K
Replies
15
Views
3K
Replies
10
Views
2K
Replies
7
Views
2K
Replies
1
Views
2K
Replies
10
Views
2K
Replies
5
Views
3K
Replies
21
Views
3K
Back
Top