7. Write a function in Python that would take 2 numbers and multiply them together and return the result. Call the function and get the result and print it to the shell (3 points).
Answer:
def multiply(num1, num2): return num1 * num2 result = multiply(10,2) print(result)
8. Write Python code that would return a random number between 1 to 14.
Answer:
import random random.randint(1,14)
9. Write a Python code that would return a random element from this list names=[‘Matthew’, ‘Mark’,’Luke’, ‘John’]
Answers:
import random names=[‘Matthew’, ‘Mark’,’Luke’, ‘John’] random.choice(names)
10. Write a function in Python that would take 5 String parameters, combine / concatenate them together and inside the function print the concatenation into the screen (3 points).
Answer:
def combine(p1, p2, p3, p4, p5): print(p1+p2+p3+p4+p5) combine('My ', 'name ', ' is', ' Gamas', ' Chang')
11. Fix above broken python codes in Python editor (Thonny). After it is fixed, copy the corrected code below (9 points)
Answer
budget1 = 50 * 5 print ('budget1 '+str(budget1)) abigail = 11 ryan = 10 budget2 = abigail * ryan print ('budget2 '+str(budget2)) hot_day = int('20') < 15 print (hot_day) if hot_day == True: print('It is really hot out there') else: print('It is cool out there') hometown = 'Moscow' print('I live in '+ hometown)
12. Write a Python code that would continously ask user in the shell “Give me a yes or no question” and print random words from this list words=[‘Yes’, ‘No’,’Maybe’] . If user enter ‘quit’, then stop from the loop.
Answer
import random words=['Yes', 'No','Maybe'] while True: question = input("Give me a yes or no question") if question == 'quit': break print(random.choice(words))