Improve a piece of code with functions

In this first week you’ve seen how to use and create functions of your own. You’ve also seen examples where functions can add structure to a program and simplify it. In this step, I’d like you to think more about when it’s appropriate to create your own functions. To do this, I’ve provided a short-ish program that currently contains no functions. I would like you to suggest functions that could be added to the program.

The program is a guessing game which asks the player to think of a number and then uses a simple strategy to guess the number. Read through the program below, or run it to see it in action.

## Guessing Game


max = 101

min = 0


print("Think of a number between 1 and 100")



middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 1 guess".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 2 guesses".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 3 guesses".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 4 guesses".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 5 guesses".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 6 guesses".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 7 guesses".format(middle))

  quit()


middle = int((max + min)/2)

answer = input("Is your number [H]igher, [L]ower or the [S]ame as {}".format(middle)).upper()

if answer == "H":

  min = middle

elif answer == "L":

  max = middle

else:

  print("Your number is {}, it took me 8 guesses".format(middle))

  quit()

Next I’d like you to suggest what functions could be added to the program. Also, is there any syntax that you’ve not come across before, can you work out from the context what that code does? Feel free to implement your ideas.

Be sure to include:

There are no right or wrong answers, so please suggest, share, and discuss with your teacher.