This course will introduce the core introduction of the Python programming ... And, the answer is only for hint . it is helpful for those student who haven't submit Assignment and who confused ... These five lines are the lines to do this particular assignment where we are ...
5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. Answer:
What is wrong with this Python loop: n = 5
while n > 0 :
print(n)
print('All done')
This loop will run forever
The print('All done') statement should be indented four spaces
There should be no colon on the while statement
while is not a Python reserved word
What does the break statement do?
Exits the program
Exits the currently executing loop
Resets the iteration variable to its initial value
Jumps to the "top" of the loop and starts the next iteration
What does the continue statement do?
Exits the program
Jumps to the "top" of the loop and starts the next iteration
Exits the currently executing loop
Resets the iteration variable to its initial value
What does the following Python program print out? tot = 0
for i in [5, 4, 3, 2, 1] :
tot = tot + 1
print(tot)
15
10
5
0
What is the iteration variable in the following Python code: friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')
friend
Glenn
Sally
friends
What is a good description of the following bit of Python code? zork = 0
for thing in [9, 41, 12, 3, 74, 15] :
zork = zork + thing
print('After', zork)
Find the smallest item in a list
Count all of the elements in a list
Sum all the elements of a list
Find the largest item in a list
What will the following code print out? smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15] :
if the_num < smallest_so_far :
smallest_so_far = the_num
print(smallest_so_far) Hint: This is a trick question and most would say this code has a bug - so read carefully
-1
74
42
3
What is a good statement to describe the is operator as used in the following if statement: if smallest is None :
smallest = value
Is true if the smallest variable has a value of -1
The if statement is a syntax error
Looks up 'None' in the smallest variable if it is a string
matches both type and value
Which reserved word indicates the start of an "indefinite" loop in Python?
for
break
while
def
indef
How many times will the body of the following loop be executed? n = 0
while n > 0 :
print('Lather')
print('Rinse')
print('Dry off!')
4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.
Answer:
Which Python keyword indicates the start of a function definition?
return
sweet
continue
def
In Python, how do you indicate the end of the block of code that makes up the function?
You add a line that has at least 10 dashes
You de-indent a line of code to the same indent level as the def keyword
You put the colon character (:) in the first column of a line
You put the "END" keyword in column 7 of the line which is to be the last line of the function
In Python what is the input() feature best described as?
A reserved word
A built-in function
The central processing unit
A user-defined function
What does the following code print out? def thing():
print('Hello')
print('There')
There
Hello
Hello There
thing Hello There
In the following Python code, which of the following is an "argument" to a function? x = 'banana'
y = max(x)
print(y)
'banana'
max
x
print
What will the following Python code print out? def func(x) :
print(x)
func(10)
func(20)
x 20
func func
x 10 x 20
10 20
Which line of the following Python program will never execute? def stuff():
print('Hello')
return
print('World')
stuff()
print ('World')
stuff()
print('Hello')
return
def stuff():
What will the following Python program print out? def greet(lang):
if lang == 'es':
return 'Hola'
elif lang == 'fr':
return 'Bonjour'
else:
return 'Hello'
print(greet('fr'),'Michael')
Bonjour Michael
Hola Michael
Hello Michael
def Hola Bonjour Hello Michael
What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b):
added = a + b
return a
x = addtwo(2, 7)
print(x)
2
14
addtwo
7
What is the most important benefit of writing your own functions?
Following the rule that no function can have more than 10 statements in it
Following the rule that whenever a program is more than 10 lines you must use a function
Avoiding writing the same non-trivial code more than once in your program
To avoid having more than 10 lines of sequential code without an indent or de-indent