All Coursera Quiz Answers

Python for everybody Week 3 Quiz Answer

Hello Friends in this article i am gone to share Coursera Course: Python Data Structures Week 3 Quiz Answers with you..

Python Data Structures

Go to this Course: Python Data Structures


Also visit this link: Python Data Structures Week 2 Quiz Answers


 

Python Data Structures Week 3 Quiz Answers

Question 1)
What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?
  • Underline all of the conditional code
  • Begin the statement with a curly brace {
  • Start the statement with a “#” character
  • Indent the line below the if statement
Question 2
Which of these operators is not a comparison / logical operator?
  • =
  • <
  • ==
  • >=
  • >
Question 3)
What is true about the following code segment:
ifย  x == 5 :
  ย  print 'Is 5'
  ย  print 'Is Still 5'
ย  ย  print 'Third 5'
  • The string ‘Is 5’ will always print out regardless of the value for x.
  • The string ‘Is 5’ will never print out regardless of the value for x.
  • Only two of the three print statements will print out if the value of x is less than zero.
  • Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
Question 4)
When you have multiple lines in an if block, how do you indicate the end of the if block?
  • You use a curly brace { after the last line of the if block
  • You capitalize the first letter of the line following the end of the if block
  • You put the colon : character on a line by itself to indicate we are done with the if block
  • You de-indent the next line past the if block to the same level of indent as the original if statement
Question 5)
You look at the following text:
if x == 6 :
  ย  print 'Is 6'
  ย  print 'Is Still 6'
ย  ย  print 'Third 6'
ย 
It looks perfect but Python is giving you an ‘Indentation Error’ on the second print statement. What is the most likely reason?
  • Python thinks ‘Still’ is a mis-spelled word in the string
  • You have mixed tabs and spaces in the file
  • Python has reached its limit on the largest Python program that can be run
  • In order to make humans feel inadequate, Python randomly emits ‘Indentation Errors’ on perfectly good code – after about an hour the error will just go away without any changes to your program
Question 6)
What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?
  • toggle
  • switch
  • else
  • A closing curly brace followed by an open curly brace like this }{
Question 7)
What will the following code print out?
x = 0
if x < 2 :
  ย  print 'Small'
elif x < 10 :
  ย  print 'Medium'
else :
  ย  print 'LARGE'
print 'All done'ย 
  • Small
  • Medium
  • All done
  • Small
  • All done
  • Small
  • Medium
  • LARGE
  • All done
ย 
Question 8
For the following code,
if x < 2 :
  ย  print 'Below 2'
elif x >= 2 : 
  ย  print 'Two or more'
else :
ย  ย  print 'Something else'
What value of ‘x’ will cause ‘Something else’ to print out?
  • x = -22
  • x = -2.0
  • This code will never print ‘Something else’ regardless of the value for ‘x’
  • x = 2.0
Question 9)
‘In the following code (numbers added) – which will be the last line to execute successfully?
(1)ย  ย astr = 'Hello Bob'
(2)ย  ย istr = int(astr)
(3)ย  ย print 'First', istr
(4)ย  ย astr = '123'
(5)ย  ย istr = int(astr)
(6)ย  ย print 'Second', istr
  • 5
  • 1
  • 6
  • 2
Question 10)
For the following code:
astr = 'Hello Bob'
istr = 0
try:
  ย  istr = int(astr)
except:
ย  ย  istr = -1
What will the value for istr after this code executes?
  • false
  • It will be a random number depending on the operating system the program runs on
  • -1
  • 9 (the number of characters in ‘Hello Bob’)

Week 3 Assignment 1

3.1 Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use raw_input to read a string and float() to convert the string to a number. Do not worry about error checking the user input – assume the user types numbers properly.
hrs = raw_input("Enter Hours:")
h = float(hrs)

rate = raw_input("Enter Rate:")
rate = float(rate)
if h <= 40:
  ย  pay = h * rate
elif h > 40:
  ย  pay = 40*rate + (h-40)*rate*1.5
print pay

ย 

Week 3 Assignment 2

3.3 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.
python code..
number = raw_input("Enter number:")
try:
  ย  number = float(number)
except:
  ย  number = -1
if number >= 0.9:
  ย  print "A"
elif number >= 0.8:
  ย  print "B"
elif number >= 0.7:
  ย  print "C"
elif number >= 0.6:
  ย  print "D"
elif number < 0.6:
  ย  print "F"
else:
  ย  print "Error!"
  ย  quit()