Question: Part1 num1 = 10 num2 = 12 num3 = num1 - num2 + 12 fin = open( myfile.txt , w ) fin.write( line 1:
Part1
num1 = 10
num2 = 12
num3 = num1 - num2 + 12
fin = open("myfile.txt", "w")
fin.write("line 1: " + str(num1) + " ")
num1 += 5
fin.write("line 2: " + str(num2) + " ")
num2 -= 11
fin.write("line 3: " + str(num3) + " ")
fin.write("line 4: " + str(num1 + num2 + num3) + " ")
fin.write("line 5: " + str(num3 - num2 + num1) + " ")
fin.close()
For the following exercises, refer to the program segment.
(1) ( File Processing )
When the above code segment is executed, and data is recorded to the output file, what value will be displayed next to the following line number in this file?
line 1:
(a) 15 (b) 24 (c) 26 (d) 10 (e) 12
(2) ( File Processing )
When the above code segment is executed, and data is recorded to the output file, what value will be displayed next to the following line number in this file?
line 2:
(a) 15 (b) 24 (c) 26 (d) 10 (e) 12
(3) ( File Processing )
When the above code segment is executed, and data is recorded to the output file, what value will be displayed next to the following line number in this file?
line 3:
(a) 15 (b) 24 (c) 26 (d) 10 (e) 12
(4) ( File Processing )
When the above code segment is executed, and data is recorded to the output file, what value will be displayed next to the following line number in this file?
line 4:
(a) 15 (b) 24 (c) 26 (d) 10 (e) 12
(5) ( File Processing )
When the above code segment is executed, and data is recorded to the output file, what value will be displayed next to the following line number in this file?
line 5:
(a) 15 (b) 24 (c) 26 (d) 10 (e) 12
Part 2 Exercises - Recursion and Random Numbers
Enter T for True or F for False.
from random import randint
n = int(input())
m = int(input())
random1 = randint(m, n)
print (random1)
Considering the above program segment, answer each of these as True or False.
_____ (1) If the user enters 10 for n and 5 for m , then the random number 6 may be displayed by the program.
_____ (2) If the user enters 0 for n and 7 for m , then the program will exhibit a run - time error.
_____ (3) If the user enters 5 for n and 5 for m , then the program will exhibit a run - time error.
_____ (4) If the user enters 6 for n and 0 for m , then the program will generate a number that appears on a face of a typical six - sided playing die.
_____ (5) If the user enters 50 for n and 1 for m , then the random number 51 may be displayed by the program.
Part 3 Programming Exercises - Recursion and Random Numbers
Select the correct answer or enter the result in the space provided.
(1) A recursive function with parameter n counts from any negative number to 0 . An appropriate base case would be n == 0 .
(a) True (b) False
(2) A recursive function can have two base cases, such as n == 0 returning 0 , and n == 1 returning 1 .
(a) True (b) False
(3) n factorial ( n! ) is commonly implemented as a recursive function due to being easier to understand and executing faster than a loop implementation.
(a) True (b) False
(4) How many times will this recursive function be called?
def count_down(count):
if count == 0:
print("Blast Off!")
else :
print(count)
count_down(count - 1)
count_down(3)
(a) 5 (b) 4 (c) 3 (d) 2 (e) 1
(5) How many times will this recursive function be called?
def factorial(n) :
if (n == 1) :
return 1
else :
return n * factorial(n - 1)
result = factorial(5)
print (result)
(a) 120 (b) 1 (c) 3 (d) 5 (e) 7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
