+ 1

Sums problems

We can easily return twice or thrice times of sum of 2 or 3 number is equal . Such as def num(x,y): sum = x+y if x==y: sum*=3 return sum print(num(5,5)) My question is if there are more than 100 or above numbers are equal how can I code the number?

12th Sep 2024, 12:20 PM
Uchawsing Marma
Uchawsing Marma - avatar
1 Resposta
+ 3
You can use args for this case. Below is an example def num(*args): # Get the sum of all numbers total_sum = sum(args) # Check if all numbers are the same if all(x == args[0] for x in args): total_sum *= 3 return total_sum # Test with 100 equal numbers print(num(*([5] * 100))) # Passing a list of 100 numbers, all equal to 5
12th Sep 2024, 1:45 PM
Aysha
Aysha - avatar