Python for everybody Week 5 Quiz Answer
Hello Friends in this article i am gone to share Coursera Course: Python Data Structures Week 5 Quiz Answers with you..
Python Data Structures
Go to this Course: Python Data Structures
Also visit this link: Python Data Structures Week 4 Quiz Answers
Python Data Structures Week 5 Quiz Answers
Question 1)
What is wrong with this Python loop:
n = 5 while n > 0 : print n print 'All done'
- This loop will run forever
- while is not a Python reserved word
- There should be no colon on the while statement
- The print ‘All done’ statement should be indented four spaces
Question 2)
What does the break statement do?
- Exits the program
- Resets the iteration variable to its initial value
- Exits the currently executing loop
- Jumps to the “top” of the loop and starts the next iteration
Question 3)
What does the continue statement do?
- 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
- Exits the program
Question 4)
What does the following Python program print out?
tot = 0 for i in [5, 4, 3, 2, 1] : tot = tot + print tot
- 0
- 5
- 10
- 15
Question 5)
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
- friends
- Sally
- Joseph
Question 6)
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
- Sum all the elements of a list
- Find the smallest item in a list
- Count all of the elements in a list
- Compute the average of the elements in a list
Question 7)
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
- 3
- 74
- 42
Question 8)
What is a good statement to describe the is operator as used in the following if statement:
if smallest is None : smallest = value
- The if statement is a syntax error
- matches both type and value
- Is true if the smallest variable has a value of -1
- Looks up ‘None’ in the smallest variable if it is a string
Question 9)
Which reserved word indicates the start of an “indefinite” loop in Python?
- while
- def
- break
- for
- indef
Question 10)
How many times will the body of the following loop be executed?
n = 0 while n > 0 : print 'Lather' print 'Rinse' print 'Dry off!'
- 1
- 5
- 0
Week 5 Assignment
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 the numbers from the book for problem 5.1 and Match the desired output as shown.
Python code…
largest = None smallest = None while True: num = raw_input("Enter a number: ") if num == "done" : break try: num = int(num) except: print "Invalid input" continue if largest is None: largest = num elif largest < num: largest = num if smallest is None: smallest = num elif smallest > num: smallest = num print "Maximum is", largest print "Minimum is", smallest