Question

In: Computer Science

import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1...

import sys

var = 1
print(var)

def function1(myVar):
    print(myVar)
    var = myVar + 1
    print(var)
    function2(var)

def function2(myVar):
    print(myVar)
    var = myVar + 1
    print(var)
    function3(var)

def function3(myVar):
    print(myVar)
    var = myVar + 1
    print(var)

def main(argv):
    var = 10
    print(var)
    function1(var)

if __name__=="__main__":
    main(sys.argv)

1. As the program runs, what is the first line that will be interpreted by Python, and what action will be executed?

2. What will be printed on line 7?

3. What will be printed on line 9?

4. What will be printed on line 13?

5. What will be printed on line 15?

6. What will be printed on line 19?

7. What will be printed on line 21?

8. What will be printed on line 25?

9. What is the scope of the variable at line 3?

10. What will happen if a command line argument other than the script name is passed to this program?

Solutions

Expert Solution

For the above questions, below answer may help you

1. As the program runs, what is the first line that will be interpreted by Python, and what action will be executed?
Answer: import sys
2. What will be printed on line 7?
Answer: print(myVar) result = 10
3. What will be printed on line 9?
Answer: print(var) result = 11
4. What will be printed on line 13?
Answer: print(myVar) result = 11
5. What will be printed on line 15?
Answer: print(var) result = 12
6. What will be printed on line 19?
Answer: print(myVar) result = 12
7. What will be printed on line 21?
Answer: print(var) result = 13
8. What will be printed on line 25?
Answer: print(var) result = 10
9. What is the scope of the variable at line 3?
Answer: Local variable
10. What will happen if a command line argument other than the script name is passed to this program?
Answer: start the execution from main method

if you want track other program in python

python -m trace --ignore-dir=../lib --trace <program_name>.py


Related Solutions

import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(")...
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(") " + entry) return int(input(question)) plz explain this code
Part 1 Here is some code: def foo(var): var = [] var.append("hello") var.append("world") list = ["bah"]...
Part 1 Here is some code: def foo(var): var = [] var.append("hello") var.append("world") list = ["bah"] foo(list) print(list) Which Option is correct? A) ["hello","world"] is output because python passes the list by reference and the list is changed inside the function. B) ["bah"] is output because python passes the list by value so changes to the list in the function are made to a separate list than the one that is passed in. C) ["bah"] is output because python passes...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
Fix this broken code? # Get our input from the command line import sys string =...
Fix this broken code? # Get our input from the command line import sys string = sys.argv[1] # Your code goes here if string 'Bingo' print('Missed') else: print('Hit!')
import random # define functions def rollDice(): # function returns a random number between 1 and...
import random # define functions def rollDice(): # function returns a random number between 1 and 6 def userWon(t1, t2): # function accepts player total and computer total # function returns true if player wins # function returns false if computer wins def main(): # each player rolls two Dice player = rollDice() + rollDice() computer = rollDice() + rollDice() # ask the player if they want to roll again again = int(input("Please enter 1 to roll again. Enter 2...
Using Python def specialAverage():    Print the number of 0s and then return the average of...
Using Python def specialAverage():    Print the number of 0s and then return the average of either the positive values in the list (if the optional parameter has value 1) or the negative values in the list (if the optional parameter has value 0).    Arguments: a list of numbers : the list can contain any numeric values an integer : this integer is an optional parameter and only takes value 0 or 1, and defaults to 1 if the...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...
def stateTax(amount): taxRate = 0.05 taxAmount = taxRate * amount print('State tax: ',taxAmount) return taxAmount def...
def stateTax(amount): taxRate = 0.05 taxAmount = taxRate * amount print('State tax: ',taxAmount) return taxAmount def countyTax(amount): taxRate = 0.025 taxAmount = taxRate * amount print('County tax: ',taxAmount) return taxAmount def totalTax(state,county): taxAmount = state + county print('Total tax: ',taxAmount) return taxAmount def totalAmount(amount, tax): total = amount + tax return total def main(): amount = float(input('Enter the amount of purchase: ')) sTax = stateTax(amount) cTax = countyTax(amount) tTax = totalTax(sTax,cTax) print('Total amount including tax is: ' ,total) main() I...
// 5. Predict what the following code will print (write it down), then try it. var...
// 5. Predict what the following code will print (write it down), then try it. var myVariable = "global"; var myOtherVariable = "global"; function myFunction() { var myVariable = "local"; return myVariable; } function myOtherFunction() { myOtherVariable = "local"; return myOtherVariable; javascript
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n (int) starting value for 3n+1 sequence return: None """ while(n != 1): print(n) if(n % 2) == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1 print(n) # the last print is 1 def main(): seq3np1(3) main() Using the provided code, alter the function as follows: First, delete the print statements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT