I am learning Python and got stuck on a task with creating program that will find smallest number of 4 using only if and else. No elif, no min, no list etc, only base if and else
I dont really have any ideas, I only made 2 programs which use elif, but they dont work too:
x=int (input())
b=int (input())
c=int (input())
d=int (input())
if x<b and x<c and x<d:
print (x)
elif b<x and b<c and b<d:
print (b)
elif c<x and c<b and c<d:
print (c)
else: print (d)
File "c:\Python\r.py", line 1, in <module>
x=int (input())
^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10
and
a=int (input())
b=int (input())
c=int (input())
d=int (input())
n1=int
n2=int
if a<b:
a=n1
else: b=n1
if c<d:
c=n2
else: d=n2
n1=int
n2=int
if n1<n2:
print (n1)
else: print (n2)
TypeError: '<=' not supported between instances of 'type' and 'type'
>Solution :
You can do it using nested if statements to test all the possibilities.
n1 = int(input())
n2 = int(input())
n3 = int(input())
n4 = int(input())
if n1 < n2:
if n1 < n3:
if n1 < n4:
print(n1)
else:
print(n4)
else:
if n3 < n4:
print(n3)
else:
print(n4)
else:
if n2 < n3:
if n2 < n4:
print(n2)
else:
print(n4)
else:
if n3 < n4:
print(n3)
else:
print(n4)