Week 4: Exercise Solutions

Exercise 4.1

# To work out the bigger of two numbers
a = int(input("First number: "))
b = int(input("Second number: "))
if a > b:
    print("The bigger is", a)
else:
    print("The bigger is", b)

Exercise 4.2

>>> print(len(str(2**100)))

You need to use str because len() will only work with strings.

Exercise 4.3

# To find the middle of a string
string = input("Enter a string: ")
if len(string) % 2 == 1:
    print("The middle of the string is ",
          string[len(string) // 2], ".", sep="")
else:
    middle = len(string) // 2
    print("The middle of the string is ",
          string[middle - 1], string[middle], ".", sep="")