Example solutions for script 03_branching

Exercise 1.2

number = int(input("please enter an integer number: "))
if number > 0:
    print(number, "is positive")
please enter an integer number: -3

Exercise 1.3

number = int(input("please enter an integer number: "))
if number > 0:
    print(number, "is positive")
else:
    print(number, "is not positive")
please enter an integer number: -3
-3 is not positive

Exercise 1.4

number = float(input("please enter a real number: "))
if 0 <= number and number <= 1.0:
    print(number, "is in the range 0.0 to 1.0")
please enter a real number: 0.5
0.5 is in the range 0.0 to 1.0

For checking if a number is in a given interval Python provides an abreviation we did not introduce in the script:

number = float(input("please enter a real number: "))
if 0 <= number <= 1.0:
    print(number, "is in the range 0.0 to 1.0")
please enter a real number: 0.5
0.5 is in the range 0.0 to 1.0

Exercise 2.3

number = float(input("please enter a real number: "))
if number < 0:
    print(number, "is negative")
elif number > 0:
    print(number, "is positive")
else:
    print("you entered zero !")
please enter a real number: 0
you entered zero !

Exercise 2.3

number = int(input("please enter an integer number: "))

if number % 3 == 0 and number % 4 == 0:
    print(number, "is a multiple of 3 and of 4")
elif number % 3 != 0 and number % 4 == 0:
    print(number, "is a multiple of 4 but not of 3")
elif number % 3 == 0 and number % 4 != 0:
    print(number, "is a multiple of 3 but not of 4")
else:
    print(number, "is neither a multiple of 4 nor of 3")
please enter an integer number: 3
3 is a multiple of 3 but not of 4

Alternative solution with less typing (but harder to understand !):

number = int(input("please enter an integer number: "))

if number % 3 != 0 and number % 4 != 0:
    print(number, "is not a multiple of 3 and 4")
elif number % 3 != 0: 
    print(number, "is a multiple of 4 but not of 3")
elif number % 4 != 0:
    print(number, "is a multiple of 3 but not of 4")
else:
    print(number, "is a multiple of 3 and 4")
please enter an integer number: 5
5 is not a multiple of 3 and 4

Exercise 2.4

hours = float(input("number of hours worked: "))
wage = float(input("wage per hour: "))

if hours <= 40:
    loan = hours * wage
elif hours <= 60:
    loan = 40 * wage + (hours - 40) * 2 * wage
else:
    loan = 40 * wage + 20 * 2 * wage + (hours - 60) * 3 * wage
print("computed loan is", loan)
number of hours worked: 61
wage per hour: 1
computed loan is 83.0

Exercise 2.5

From wikipedia: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.

n = 1900

if n % 400 == 0:
    print(n, "is a leap year")
elif n % 100 == 0:
    print(n, "is not a leap year")
elif n % 4 == 0:
    print(n, "is a leap year")
else:
    print(n, "is not a leap year")
1900 is not a leap year

Alternative solution (introduced paranthesis to make the logical computation more readable):

n = 2000
is_leap = (n % 400 == 0) or ((n % 4 == 0) and not (n % 100 == 0))
print(is_leap)
True

Exercise 3.1

a = input("move of player a: ")
b = input("move of player b: ")

if a == b:
    print("its a tie")
elif a == "R" and b == "S" or a == "S" and b == "P" or a == "P" and b == "R":
    print("a wins")
else:
    print("b wins")
move of player a: R
move of player b: P
b wins

Exercise 3.2

a = input("move of player a: ")
b = input("move of player b: ")

if a != "R" and a != "S" and a != "P":
    print("invalid input for player a")
elif b != "R" and b != "S" and b != "P":
    print("invalid input for player b")
elif a == b:
    print("its a tie")
elif a == "R" and b == "S" or a == "S" and b == "P" or a == "P" and b == "R":
    print("a wins")
else:
    print("b wins")
move of player a: S
move of player b: R
b wins
# a bit simpler using string addition trick:

a = input("move of player a: ")
b = input("move of player b: ")

ab = a + b

if a != "R" and a != "S" and a != "P":
    print("invalid input for player a")
elif b != "R" and b != "S" and b != "P":
    print("invalid input for player b")
elif a == b:
    print("its a tie")
elif ab == "RS" or ab == "SP" or ab == "PR":
    print("a wins")
else:
    print("b wins")
move of player a: R
move of player b: S
a wins

Addition to the posed exercise

The solution above did not show messages for a and b if both inputs were invalid.

The extended solution fixes this, please try to understand it

a = input("move of player a: ")
b = input("move of player b: ")

# brackets not required, but they make the code more readable:
a_correct = (a == "R" or a == "S" or a == "P")
b_correct = (b == "R" or b == "S" or b == "P")

ab = a + b

if not a_correct:
    print("invalid input for player a")
if not b_correct:
    print("invalid input for player b")

if a_correct and b_correct:
    if a == b:  # why no chec for b_correct here ?
        print("its a tie")
    elif ab == "RS" or ab == "SP" or ab == "PR": # no check for a_correct required. why ?
        print("a wins")
    elif a_correct and b_correct:
        print("b wins")
move of player a: R
move of player b: R
its a tie