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

Write a program to find the greatest of the three numbers entered by the user

Hello i need help in this code, it is given in my book like this but i am getting error. I am a school student and a new programmer.

#Program to find the greatest of the three numbers entered by the user
num1 = input('Enter the first number\t:')
num2 = input('Enter the second number\t:')
num3 = input('Enter the third number\t:')
if int(num1) > int(num2):
    if int(num1) > int(num3):
         big = int(num1)
    else:
        big = int(num2)
else:
    if int(num2) > int(num3)
    big = int(num2)
    else:
        big = int(num3)
print(big)

>Solution :

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 missing char and a wrong choice between num2 and num3 :

if int(num1) > int(num2):
    if int(num1) > int(num3):
        big = int(num1)
    else:
        big = int(num3)       # should be num3
else:
    if int(num2) > int(num3): # was missing the ':'
        big = int(num2)
    else:
        big = int(num3)

Improve by converting to int once

num1 = int(input('Enter the first number\t:'))
num2 = int(input('Enter the second number\t:'))
num3 = int(input('Enter the third number\t:'))

If needed, condition can be shortened ifTrue if condition else ifFalse

if num1 > num2:
    big = num1 if num1 > num3 else num3
else:
    big = num2 if num2 > num3 else num3

# or 

big = (num1 if num1 > num3 else num3) if num1 > num2 else (num2 if num2 > num3 else num3)
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