Home › Forums › Assignment courserra › Google IT in Automation with Python Professional Certificate › Crash Course on Python › Practice Quiz “For Loops”
Tagged: Coursera, Crash Course, Google, Python
 This topic has 2 replies, 1 voice, and was last updated 11 months, 2 weeks ago by Abhishek Tyagi.

AuthorPosts


July 14, 2020 at 2:07 pm #938Abhishek TyagiKeymaster
1. How are while loops and for loops different in Python?
Answer
While loops iterate while a condition is true, for loops iterate through a sequence of elements.2. Fill in the blanks to make the factorial function return the factorial of n.
Then, print the first 10 factorials (from 0 to 9) with the corresponding number.
Remember that the factorial of a number is defined as the product of an integer and all integers before it. For example,
the factorial of five (5!) is equal to 1*2*3*4*5=120. Also, recall that the factorial of zero (0!) is equal to 1.Answer
def factorial(n): result = 1 for x in range(1,n+1): result = result * x return result for n in range(0,10): print(n, factorial(n+0))
3. Write a script that prints the first 10 cube numbers (x**3), starting with x=1 and ending with x=10.
Answer
for x in range(1,11): print(x**3)
 This topic was modified 11 months, 2 weeks ago by Abhishek Tyagi.

July 14, 2020 at 2:10 pm #940Abhishek TyagiKeymaster
5. Write a script that prints the multiples of 7 between 0 and 100.
Print one multiple per line and avoid printing any numbers that aren’t multiples of 7.
Remember that 0 is also a multiple of 7Answer
def retry(operation, attempts): for n in range(attempts): if operation(): print("Attempt " + str(n) + " succeeded") break else: print("Attempt " + str(n) + " failed") retry(create_user, 3) retry(stop_service, 5)

July 14, 2020 at 2:13 pm #941Abhishek TyagiKeymaster
4. Write a script that prints the multiples of 7 between 0 and 100. Print one multiple per line and avoid printing any numbers that aren’t multiples of 7. Remember that 0 is also a multiple of 7.
Answer
`for num in range(0,100):
if(num % 7==0):print(num)else:num= num + 1`


AuthorPosts
 You must be logged in to reply to this topic.