0% found this document useful (0 votes)
16 views

Python Refute Code[RA2411003011193]

Uploaded by

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

Python Refute Code[RA2411003011193]

Uploaded by

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

SRM Institute of Science and Technology

Department of Computing Technologies


21CSS101J PROGRAMMING FOR PROBLEM-SOLVING

LLJ2 – REFUTE QUESTIONS

A REFUTE QUESTION REPORT - Python

Submitted by

bonagiri hanumanthu naga sai hasith [Reg. No: ]


Problem Statement 1:

Finds the second largest number in a list.

Code:

nums = list(map(int, input("Enter numbers separated by spaces: ").split()))

# Initializing largest and second largest to a very small value

#'-inf' is the samllest possible value

largest = second_largest = float('-inf')

for num in nums:

if num > largest:

second_largest = largest

largest = num

elif num > second_largest and num != largest:

second_largest = num

print(f"Second largest number: {second_largest}")

Output:

Error: The error occurs when there are duplicate largest numbers in the list. In such a case,
the second_largest remains as -inf

Solution:

Giving out if-else conditions with giving ‘same number entered’

Code:
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))

# Check if there are at least two numbers in the list

if len(nums) < 2:

print("List should have at least two numbers")

else:

# Initialize largest and second largest to negative infinity

largest = second_largest = float('-inf')

# Loop through the numbers to find the largest and second largest

for num in nums:

if num > largest:

second_largest = largest

largest = num

elif num > second_largest and num != largest:

second_largest = num

# Check if second largest number was found

if second_largest == float('-inf'):

print("Same number is entered")

else:

print(f"Second largest number: {second_largest}")

Output:
Refuge:

The error occurs when there are duplicate largest numbers in the list. In such a case, the
second_largest remains as -inf, which triggers the "same number is entered" message.
Problem Statement 2:
An code with indentation error.

Code:

def greet_user(name):
print(f"Hello, {name}!")
print("Welcome to the Python world!") # Indentation error here

def main():
user_name = input("Enter your name: ")
greet_user(user_name)

if __name__ == "__main__":
main()

Output:

Error: The second print("Welcome to the Python world!") is not properly indented. It should
be inside the greet_user function but is indented incorrectly, leading to an IndentationError.
In Python, the body of a function or control structure must be indented consistently.

Code:

def greet_user(name):
print(f"Hello, {name}!")
print("Welcome to the Python world!") # Correct indentation
def main():
user_name = input("Enter your name: ")
greet_user(user_name)

if __name__ == "__main__":
main()

Output:

You might also like