Robotics & Artificial Intelligence
Write a function called ice_water that takes a number as input. Perform the following operations:
- If the number is divisible by 3, then it should return "ice".
- If it is divisible by 5, then it should return "water".
- If it is divisible by both 3 and 5, then it should return "icewater".
- Otherwise, it should return the same number.
Python Functions
2 Likes
Answer
def ice_water(n):
if n % 3 == 0 and n % 5 == 0:
return "icewater"
elif n % 3 == 0:
return "ice"
elif n % 5 == 0:
return "water"
else:
return n
num = int(input("Enter a number: "))
result = ice_water(num)
print(result)Output
Enter a number: 15
icewater
Enter a number: 9
ice
Enter a number: 20
water
Answered By
3 Likes
Related Questions
Is the return statement mandatory for every function?
Write a function that returns the greater of two numbers passed as arguments.
Write a function for checking the speed of drivers. This function should have one parameter: speed.
- If speed is less than 70, it should print "OK".
- Otherwise, for every 5 km above the speed limit (70), it should give the driver one demerit point. Calculate and print the total number of demerit points at the end. For example, if the speed is 80, it should print: "Demerit Points: 2".
- If the driver gets more than 12 demerit points, the function should print: "License suspended".
Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if the limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.