Python Forum
help me simple code result min and max number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help me simple code result min and max number
#1
I have the following code:

largest = None
smallest = None
while True:
    num = input('enter a num ')
    if num == "done":
        break
    try:
        val = float(num)
    except:
        print("Invalid input")
        continue
    if largest is None or num > largest:
        print("setting largest to ", num)
        largest = num
        print("largest: ", largest)
        print('***********')
    if smallest is None or num < smallest:
        print("setting smallest to ", num)
        smallest = num
        print("smallest: ", smallest)
        print('***********')
print("Maximum is", largest)
print("Minimum is", smallest)
which produces the following output in the terminal

Output:
enter a num 7 setting largest to 7 largest: 7 *********** setting smallest to 7 smallest: 7 *********** enter a num 2 setting smallest to 2 smallest: 2 *********** enter a num bob Invalid input enter a num 10 setting smallest to 10 smallest: 10 *********** enter a num 4 enter a num done Maximum is 7 Minimum is 10
two questions:

1. If I remove the continue than the largest is set to bob? Why? Why do I even need the continue? I would never expect bob to be set to the largest as largest has already been set to 7 previously (it can not be None) and bob can not be greater than largest

if largest is None or num > largest:
7 is None || "bob" > 7
False || False
I do not understand how that statement can be truthy

2. How does the final smallest get set to 10 ? it was last set to 2
is 10 less than 2?

please explain these things

In my language the below image works exactly as expected so why isnt the above code working as expected?

   
Reply


Messages In This Thread
help me simple code result min and max number - by abrahimusmaximus - Nov-12-2022, 04:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple code not working properly tmv 2 476 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  Printing the code line number arbiel 6 1,567 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Help with simple code JacobSkinner 1 1,296 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 1,227 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Simple encoding code ebolisa 3 2,261 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  Can a program execute code in iPython shell and get result? deanhystad 3 2,710 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 2,358 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 2,553 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 4,752 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 2,035 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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