last day (15 days later) » 

08:26
hello @johnrennie sir
08:44
@PrateekMourya Hi :-)
question says
write a program to find the greatest of four numbers in oython
a1=int(input("enter a number to compare"))
a2=int(input("enter a number to compare"))
a3=int(input("enter a number to compare"))
a4=int(input("enter a number to compare"))
l1=[a1,a2,a3,a4]
l1.sort()
print("greatest number among the entered numbers is ",l1[-1])
this is what i wrote
but the solution code was
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))

if(num1>num4):
f1 = num1
else:
f1 = num4

if(num2>num3):
f2 = num2
else:
f2 = num3

if(f1>f2):
print(str(f1) + " is greatest")
else:
print(str(f2) + " is greatest")
code 1 can sort any number of numbers entered
There are many ways to write a program. Your program is fine, and the solution also works.
using if else ladder
in a different way
The solution is very efficient because it is effectively doing a binary search.
how can we accomplish what code 1 does because solution code is long to write in case of many numbers
08:51
I would use a loop. Something like:
maxnum = -math.inf
while True:
    newnum = int(input("Enter a number or 0 to stop: "))
    if newnum == 0:
        break:
    else if newnum > maxnum:
        maxnum = newnum
print(f"Maximum is {maxnum}")
syntax error
Let me try ...
import math

maxnum = -math.inf
while True:
    newnum = int(input("Enter a number or 0 to stop: "))
    if newnum == 0:
        break
    elif newnum > maxnum:
        maxnum = newnum
print(f"Maximum is {maxnum}")
Try that ...
what is "math"
In Python functions are often gathered into groups called modules
yes which module is this
09:00
For example there is a module called math that has mathematical functions like sin(), log(), etc
I used it because I wanted to start my variable maxnum at -∞ so that any number the user entered must be bigger.
And the math module has a constant math.inf that means ∞
ah yes
got it

last day (15 days later) »