Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with simple code
#1
I have a project where I have to provide a code that calculates the largest and smallest of three integer values given. I think that I have the code pretty much written out but for some reason when I run it, the largest output never reads back correctly. I am not sure what is wrong and could use a bit of help...

# LargeSmall.py - This program calculates the largest and smallest of three integer values.

# initialize variables here.
firstNumber = -50;

secondNumber = 53;

thirdNumber = 78;

largest = None
smallest = None

# Write assignment, if, or if else statements here as appropriate.
if firstNumber > secondNumber:
    if firstNumber > thirdNumber:
        largest = firstNumber
elif secondNumber > firstNumber:
    if secondNumber > thirdNumber:
        largest = secondNumber
else:
    largest = thirdNumber

if firstNumber < secondNumber:
    if firstNumber < thirdNumber:
        smallest = firstNumber
elif secondNumber < firstNumber:
    if secondNumber < thirdNumber:
        smallest = secondNumber
else: 
    smallest = thirdNumber

# Output largest and smallest number.
print("The largest value is " + str(largest))
print("The smallest value is " + str(smallest))
deanhystad write Mar-18-2024, 05:49 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
There are holes in your logic. For brevity I'm using A, B and C in place of firstValue, secondValue and thirdValue.

These are holes in your logic for largest.
No value is assigned if A > B and C > A
Incorrect value is assigned if A = B and A > C

And these are the holes for smallest.
No value is assigned if A < B and C < A
Incorrect value is assigned if A = B and A < C

I think your approach is wrong. Nested if statements should be avoided. They can be confusing (as you know) and they chew up horizontal spacing rather quickly. I would solve this problem without using else or elif. The resulting program is shorter than yours and easier to understand.

Instead of this:
print("The largest value is " + str(largest))
You should do this:
print("The largest value is", largest)
For performance reasons you should avoid using "+" to concatenate strings. Print works fine for this example. For more complex output you should use format strings (f'strings).
print(f"The largest value is {largest}, the smallest is {smallest}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 491 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 914 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,462 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,817 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,341 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,620 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,854 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 3,062 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Help with isinstance command (very simple code) Laplace12 2 2,014 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  Simple code help bntayfur 2 1,794 Jul-05-2020, 07:47 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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