Nov-12-2022, 04:45 AM
I have the following code:
which produces the following output in the terminal
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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) |
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?