Determine the values of each of the variables and their data types after the following code is run.
a = 17
b = 5.0
c = b - a
d = b * c
e = (a != d)
f = not(not(e))
g = e and not(f)
b = d
Solution: We will keep track of each value after each step using the table below:
Initial | Line 1 | Line 2 | Line 3 | Line 4 | Line 5 | Line 6 | Line 7 | Line 8 | |
---|---|---|---|---|---|---|---|---|---|
None | |||||||||
None | None | ||||||||
None | None | None | |||||||
None | None | None | None | ||||||
None | None | None | None | None | |||||
None | None | None | None | None | None | ||||
None | None | None | None | None | None | None |
Thus, after the code is run, we have that
Let
a + b - c
a * (c / b)
(b ** b) + (9 // 4)
(a % b) - (a * b * c)
Solution:
a+b-c
25+4-9
29-9
20
a*(c/b)
25*(9/4)
25*(2.25)
56.25
(b**b)+(c//a)
(4**4)+(9//4)
256+2
258
(a%b)+(a*b*c)
(25%4)+(25*4*9)
1-(100*9)
1-900
-899
Let
not(a == c)
(b <= c) and (d > b)
not((a != b) or (d == 0))
Solution:
not(a==c)
not(3==3.0)
not(True)
False
(b<=c) and (d>b)
(-8<=3.0) and (0>-8)
True and True
True
not((a!=b) or (d==0))
not((3!=-8) or (0==0))
not(True or True)
not(True)
False
The volume of a rectangular prism is determined by multiplying
the length, width and height of the
rectangular prism by each other. Write a program called
rec_prism_volume
that inputs the length, width and height of a
rectangular prism, and outputs the volume of the rectangular prism.
Solution:
def rec_prism_volume(length, width, height):
volume = length * width * height
return volume
There are approximately
km_to_miles
, inputs a
distance in kilometres and outputs the equivalent distance in
miles
miles_to_km
, inputs a
distance in miles and outputs the equivalent distance in
kilometres
Use these two programs to make the following conversions. Round to
Solution:
def km_to_miles(km_distance):
mile_distance = km_distance / 1.609344
return mile_distance
def miles_to_km(mile_distance):
km_distance = mile_distance * 1.609344
return km_distance
Suppose a person writes code that prints the sum of any two inputted numbers. Their code is shown below, but it contains an error. Determine if it is a syntax error or a semantic error, and specify where it occurs. How could this code be fixed? (Hint: Run it through Python Tutor).
num1 = input('Enter a number:')
num2 = input('Enter another number:')
sum = num1 + num2
print('The sum of', num1, 'and', num2, 'is', sum)
Solution: This code contains a semantic error because it uses incorrect logic. The error occurs at the line
sum = num1 + num2
Since the values of num1
and
num2
are user inputs, the data
types of these values is str
. So, when the third line
of code runs, we have two strings being added together, which results in
the value of sum
being the value of
num1
followed by the value of num2
. (e.g. If num1 = "7"
and num2 = "6"
, then sum = "76"
.
We can fix this error by adding two lines of code, before sum
is defined, where the values of num1
and num2
are converted to int
. The correct code is shown below.
num1 = input('Enter a number:')
num2 = input('Enter another number:')
num1 = int(num1)
num2 = int(num2)
sum = num1 + num2
print('The sum of', num1, 'and', num2, 'is', sum)
The following code contains a total of
a == input("Enter a number:")
b = int(a)
c = a + b
a = int(a)
d = 0
e = a / d
Solution: Here are the syntax errors in the code:
The first line uses ==
and not =
, which means that instead of
defining the value of
The third line adds the value of str
, and the
value of int
, which causes a syntax error.
The sixth line divides the value of