Python Quiz Thursday

There will be a short Python quiz on Thursday 27th November. The questions will be of the format “Write a function that…” as in the examples below. You will be expected to use basic constructs such as loops and conditionals but I won’t expect you to remember particular features of the language such as string or list access methods. However, extra information may be given as part of the question to help you answer. Here are some examples of the level of programming I will be expecting.

Loop through a list.
Write a function called “findLargest” that takes a list of numbers and returns the largest number.

def findLargest(lst):
  max = 0
  for itm in lst:
    if itm > max:
      max = itm
  return max

Use basic conditionals (if, elif, else).
Write a function called “biggest” that takes two numbers and returns the larger of the two.

def biggest(a, b)
  if a > b:
    return a
  else:
    return b

Keep looping while a condition is true.
Write a function called “echo” that asks the user for input and keeps repeating whatever they type, unless they type “x”, which quits the function.

def echo:
  s = input("Enter something: ")
  while s != "x":
    print("You entered: " + s)
    s = input("Enter something: ")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s