0% found this document useful (0 votes)
8 views6 pages

RA2411027010 Python

The document provides Python code to find perfect numbers up to a specified limit and to determine the maximum and minimum values from a list of integers. It includes functions for checking perfect numbers and finding max/min values, along with examples of correct and incorrect input cases. Explanations for failures in input formats are also included, highlighting the need for valid integer inputs.

Uploaded by

dadsgift410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

RA2411027010 Python

The document provides Python code to find perfect numbers up to a specified limit and to determine the maximum and minimum values from a list of integers. It includes functions for checking perfect numbers and finding max/min values, along with examples of correct and incorrect input cases. Explanations for failures in input formats are also included, highlighting the need for valid integer inputs.

Uploaded by

dadsgift410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Question: To find all perfect numbers up to a given limit

In python code:

# Function to check if a number is perfect


def is_perfect_number(num):

sum_of_divisors = 0

# Find all divisors of num and sum them

for i in range(1, num // 2 + 1):


if num % i == 0:

sum_of_divisors += i

# A perfect number is equal to the sum of its proper divisors

return sum_of_divisors == num

# Function to find all perfect numbers up to a limit

def find_perfect_numbers(limit):
print(f"Perfect numbers up to {limit} are:")

for i in range(1, limit + 1):


if is_perfect_number(i):

print(i, end=" ")


print()

# Input and output


if __name__ == "__main__":
limit = int(input("Enter the limit: "))

find_perfect_numbers(limit)

Correct test case: input=30; output=6,28


Refute test case: input=6,60; output=invalid

Explanation of Failure:
The input() function reads input as a string. When you entered 6,60, it read it
as a string, and the int() function cannot convert a string with a comma into an
integer.

The program expects a single integer value as input, but your input format was
incorrect.

Question: To find maximum and minimum of some values


In python:

# Function to find the maximum and minimum of a list


def find_max_min(numbers):

maximum = max(numbers)
minimum = min(numbers)
return maximum, minimum

# Input values
try:

numbers = list(map(int, input("Enter numbers separated by spaces:


").split()))
if len(numbers) == 0:

print("No numbers provided!")


else:

maximum, minimum = find_max_min(numbers)


print(f"Maximum value: {maximum}")

print(f"Minimum value: {minimum}")


except ValueError:

print("Invalid input! Please enter integers only.")


Correct test case: input=90 60; output= Maximum value: 90
Minimum value:60

Refute test case: input=y u; output=invalid

Explanation of Failure:
The output indicates an "Invalid input" message because the user entered
non-integer values ("y u"). In the code, the map(int, input(...).split()) line
attempts to convert each input value to an integer. Since "y" and "u" cannot
be converted to integers

You might also like