number = int(input("please enter an integer number: "))
if number > 0:
print(number, "is positive")
number = int(input("please enter an integer number: "))
if number > 0:
print(number, "is positive")
else:
print(number, "is not positive")
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")
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")
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 !")
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")
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")
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)
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")
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)
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")
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")
# 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")
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")