I want to test if the input is a multiple of 4 or not, and if it's not, then simply round UP the input to the nearest multiple of 4 (16, 20, 24, etc depending where the input number is).
Please have a look at the following code:
num = input("Enter a number: ")
if num != #a multiple of 4:
num = round(num) #BUT round it up to the nearest multiple of 4, not just any number
any help would be appreciated, thanks!
CodePudding user response:
the following code should work:
num = int(input("Enter a number: "))
if num%4 != 0:
num = 4 * (num//4 1)
CodePudding user response:
def round_up(number: int) -> int:
return number (-number % 4)
As an extension you could parametrise that 4
in the function to make is more generic.
CodePudding user response:
You could use module
%
with 4 is zero to test if number is multiple of 4:
num = int(input("Enter a number: ")) #add int function to convert entered value into number
if num % 4 = 0:
print(f'{num} is multiple of 4 ')
else:
print(f'nearest multiple of 4 of {num} is: {num (-num % 4)}')
num (-num % 4)
is the nearest multiple of 4 that num
.
CodePudding user response:
Here's an easy implementation:
#Create a function to round number
def round_to_multiple(number, multiple):
return multiple * round(number / multiple)
#Implementation
num = int(input("Enter a number: "))
if (num%4 != 0): #a multiple of 4:
print(round_to_multiple(num,4))