Executing code, which statements are true?

In summary, the conversation discusses two questions related to a Python code. The first question involves the execution of a code which adds a value to a variable called degrees and the possible outcomes after the execution. The correct answers are that b) two variables called degrees have been created and c) the value of degrees in the function namespace is 10. The second question involves a function that prints out a statement using the variable degrees and the possible outcomes after the execution. The correct answers are that b) this code generates an error, and c) a line of output is printed from inside the function.
  • #1
hnnhcmmngs
19
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
  • #2
Which computer language is this for?
 
  • #3
berkeman said:
Which computer language is this for?
Oh I'm sorry I should have included that! It's Python.
 
  • #4
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
  • #5
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?
 
  • #6
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)?
 
  • #7
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?
 
  • #8
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.
 
  • #9
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?
 

What does it mean to execute code?

Executing code refers to the process of running a computer program or script. It involves the computer interpreting and carrying out the instructions written in the code, resulting in a specific outcome or action.

What is the difference between compiling and executing code?

Compiling code involves converting the code into machine-readable instructions, while executing code involves actually running the code and producing a desired outcome. Compiling is typically done before execution, and the compiled code is then executed.

What is the purpose of executing code?

The purpose of executing code is to automate tasks, solve problems, and create applications or software. By writing and executing code, we can instruct computers to perform specific actions and processes, making our work more efficient and effective.

What are some common programming languages used for executing code?

Some common programming languages used for executing code include Python, Java, C++, JavaScript, and Ruby. Each language has its own syntax and features, but they all serve the purpose of writing and executing code.

What are some potential errors that can occur when executing code?

Some potential errors that can occur when executing code include syntax errors, logical errors, and runtime errors. These can be caused by mistakes in the code, unexpected inputs, or issues with the computer or programming environment.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
670
Back
Top