Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python 3 program finding smallest number between 4 inputs

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading