Python Forum
I'm getting a wrong answer don't know where the bug is
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm getting a wrong answer don't know where the bug is
#1
I've written a program that inputs three integers and calls functions to determine the smallest and the largest of the three. But sometimes my program gives the wrong answer. I have written this program in C# the same way and there is no bugs.

In this Python version if I enter the integers 1, 3, and 6 my program reports that the smallest is 1 and the largest is 3.

Here is what I've currently got:

def smallest(a, b, c):
    smallest = a

    if b < smallest:
        smallest = b
    elif c < smallest:
        smallest = c

    return smallest

def largest(a, b, c):
    largest = a

    if b > largest:
        largest = b
    elif c > largest:
        largest = c

    return largest

x = int(input("Enter first number:"))
y = int(input("Enter second number:"))
z = int(input("Enter third number:"))

print()

smallest_integer = smallest(x, y, z)
largest_integer = largest(x, y, z)

print("The smallest is", smallest_integer)
print("The largest is", largest_integer)
Reply
#2
maybe this

def smallest(a, b, c):
    smallest = a
 
    if b < smallest:
        smallest = b
    elif c < smallest and b > c:
        smallest = c
 
    return smallest
 
def largest(a, b, c):
    largest = a
 
    if b > largest and b > c:
        largest = b
    elif c > largest and b < c:
        largest = c
 
    return largest
 
x = int(input("Enter first number:"))
y = int(input("Enter second number:"))
z = int(input("Enter third number:"))
 
print()
 
smallest_integer = smallest(x, y, z)
largest_integer = largest(x, y, z)
 
print("The smallest is", smallest_integer)
print("The largest is", largest_integer)
Reply
#3
I found a solution. You can't use elif. You have to only use if statements. As soon as Python finds the condition in the if statement to be true, it will skip the remaining elif statement.
Reply
#4
when entering the integers 1, 3, and 6 the program reports that the smallest is 1 and the largest is 3 because in the function largest the if checks for 3 being larger than 1 which it is so it sets largest to 3, because the following condition is a elif this doesn't happen because the first if was true. if the second elif was an if it would calculate the largest as 6.
Reply
#5
Why not using min and max?
Lesser code > lesser bugs.

def smallest(a, b, c):
    return min(a, b, c)


def largest(a, b, c):
    return max(a, b, c)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,564 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  why don't i get the answer i want CompleteNewb 12 3,398 Sep-04-2021, 03:59 PM
Last Post: CompleteNewb
  I am getting the wrong answer, and not sure why riskeay 3 2,048 Nov-05-2020, 08:24 PM
Last Post: deanhystad
  Make the answer of input int and str enderfran2006 2 2,008 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  Keeps looping even after correct answer mcesmcsc 2 1,910 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  python gives wrong string length and wrong character thienson30 2 3,003 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  How to answer subprocess prompt Monty 8 17,373 Feb-14-2018, 09:59 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020