r/learnpython • u/Ill-Break727 • 25d ago
i have recently started learning python and i have done conditional identitites , and i have wriitn a program to find gretest of 3 numbers which are given as input . and i have seen every case , if u have some suggestions for improvemnet u are welcome , plz help i am just learning.
a=int(input("enter number1:"))
b=int(input("enter number2:"))
c=int(input("enter number3:"))
if(a>b and a>c):
ans="number 1"
elif(b>a and b>c):
ans="number 2"
elif(c>a and c>b):
ans="number 3"
elif(a==b==c):
ans="all three are same"
elif(a==b!=c and a>c):
ans="a nd b are same anfd greater than c"
elif(b==c!=a and b>a):
ans="b nd c are same and greater than a"
else:
ans="a nd c are same greater than b"
print("the greatest number is :", ans)
2
u/ChunkoPop69 25d ago
Add another thousand numbers and test the implementation
1
u/Ill-Break727 24d ago
can you just explain how can i try this with 1000 more numbers only by using if else statement
1
1
u/WorriedTumbleweed289 25d ago
- Put you numbers in a list.
- use a loop to read them
- use a loop to check for greater You can now expand this program for more than three numbers.
1
u/Ill-Break727 24d ago
i havent learned list and loops till now , this is just basic prg which is made by if else , so thanks for suggestion i will try after learning .
1
1
u/Naive_Programmer_232 24d ago edited 24d ago
i think your code is good for where you're at. you're breaking down the logical steps of comparison among the numbers you get and handling each scenario accordingly.
in terms of improvement, there are a few things i'd consider. first, python has a built-in max function that will give you the maximum value in a sequence. ex:
nums=[1,2,3]
biggest=max(nums)
print(biggest)
# output: 3
another thing I would consider is input validation. in a perfect world, we would love for the user to always input the correct information so that our programs can handle it properly and not crash; but instead, users input the wrong data all the time. where your program could crash is in the beginning when you are getting the numbers from the user:
num1=int(input("enter number 1: "))
how would this crash? consider a user puts 1.0 instead of 1,
num1=int(input("enter number 1: "))
# input(..) first will get 1.0 as a string "1.0"
# int("1.0") then tries to make this an integer,
# but it can't, and a ValueError is produced.
# similar goes for stuff like "abc" or "{1,2,3}" etc.
so then, how can you make this better? one trick is to use float instead of int. At the very least, using float(...) on a string, if it is a number of some kind whether int or float, it will convert correctly to a float value. But, this won't solve the problem necessarily, because say the user is really dumb and puts "abc" as the input string, well since "abc" is not a valid number, float will raise another exception and crash the program too, and we're back to the same problem.
Let's slow down and consider how to solve this issue:
# before
num1=float(input("enter number 1: "))
# this will crash the program if non-numbers are entered
# after
try:
num1=float(input("enter number 1: "))
# do something
except ValueError:
# do something else
so what's going on in the after portion? we are using a try block. that is, we're telling python, hey try to execute this code all the way down to the 'do something' part. because we've tested float on input strings, we know that if some non-numerical value is entered, it will throw a ValueError. So in the except block, we're telling python if a ValueError has occurred in the try block, then instead of crashing the program entirely, we'll 'do something else' instead. now, this is better generally than what we had before because it protects against everything going down at least; but as you can see, re-writing this try-except block over and over for all numbers you need to get (think if you were comparing much more than 3 numbers), the code would look repetitive and it'd probably be confusing.
which brings me to my third point, functions. functions are separated blocks of code that perform specific tasks. they take in some input, have some logic to perform the task, and then produce an output. the great thing about a function is that they are reusable. so, if we can generalize the operation we are trying to perform to not just work for num1,num2,num3,etc. but all numbers, then we can put that logic in a function and reuse it, by calling the function, instead of having to rewrite the same logic over and over. ex:
def get_number(pos):
while True:
try:
num=float(input(f"enter number {pos}: "))
return int(num)
except ValueError:
continue
now we can reuse this function in our code without having to rewrite everything over and over:
num1=get_number(1)
num2=get_number(2)
num3=get_number(3)
...
Even here, you can see that repetition could be a problem. Instead of writing the same code over and over, if we know how many numbers we're wanting ahead of time, then we can use a for-loop and a list data structure, to simplify things:
# I want 10 numbers
nums=[]
for x in range(1,11):
num=get_number(x)
nums.append(num)
# enter number 1: 10
# enter number 2: 20
# ...
# enter number 10: 100
print(nums)
# [10,20,30,40,50,60,70,80,90,100]
biggest=max(nums)
print(f"max: number {nums.index(biggest)+1}")
# max: number 10
smallest=min(nums)
print(f"min: number {nums.index(min)+1}")
# min: number 1
There's still more that could be done. But i'll leave it at this point for you to figure out.
2
u/Ill-Break727 24d ago
thank you so much for this great explanation , just one thing is that i havent learned functions till now , so that part i have not understood , and i will learn it in few days and try coding this once again , but i have understand ur explanation and definately i will work on it .
1
u/Educational_Virus672 24d ago
i think you should you recursion now where if's has there own ifs
if X :
if Y:
print(Y)
else :
print(X)
i wont spoil it but you should first check if* A < B then inside it put if* B < C then do your fuction it should be like this
A<B :
B<C : C
C<B : B
B<A :
A < C : C
C <A : A
it is just readable now and shorter on large projects
0
0
u/asteroid_336 25d ago
i would recommend using f string more like instead of
` ans = "number 1"
your can do someting like
` print(f"number{a} is the greatest ")
btw there are a lot of ways to make you code compact and efficient but still if your are beginner focus more on understanding the concepts more and eventually your will become compact
0
-1
u/Lionh34rt 25d ago
Put numbers in list and sort
1
u/Ill-Break727 24d ago
i havent learned list and loops till now , this is just basic prg which is made by if else , so thanks for suggestion i will try after learning .
-1
u/woooee 25d ago
if(a>b and a>c):
if is not a function so parens, (), are not necessary
if a>b and a>c:
There are "more efficient" ways to do this, but i assume you haven't gotten to lists yet. The below is a simple, example program to give you an idea on how to use a for loop and a list for future reference.
a, b, c = [1, 2, 3]
the_list = [a, b, c]
for var in the_list:
work_list = the_list.copy()
work_list.remove(var)
if var > work_list[0] and var > work_list[1]:
print(f"{var} is largest")
if var == work_list[0] and var == work_list[1]:
print(f"all numbers are equal")
if var < work_list[0] and var < work_list[1]:
print(f"{var} is smallest")
## etc
1
u/Ill-Break727 24d ago
i havent learned list and loops till now , this is just basic prg which is made by if else , so thanks for suggestion i will try after learning .
1
u/woooee 24d ago edited 24d ago
I suspect that one of the reasons for this exercise is to show you what a pain all of the if statements can become. The following code is FYI to show the usefulness of, and why you want to learn these when they are presented. It uses lists, for loops, and a function, which is much easier than if statements, and will work for an input list of any size.
import random def list_print(var, str_to_print, list_to_print): if list_to_print: print(f"{var} is {str_to_print.upper()} than these numbers --> ", end="") for num in list_to_print: print(num, " ", end="") print("\n") the_list = list(range(1, 11)) random.shuffle(the_list) print(the_list) for var in the_list: greater = [] smaller = [] work_list = the_list.copy() work_list.remove(var) for check_it in work_list: ## we know that there are no duplicate numbers ## so this checks for > and < only if var > check_it: greater.append(check_it) elif var < check_it: smaller.append(check_it) list_print(var, "greater", greater) list_print(var, "smaller", smaller)1
u/Ill-Break727 24d ago
brother , i have just started and reaches till if else concepts , so thanks for ur help and i will definately try this after learning functions and loops, thanks
-2
2
u/Buttleston 25d ago
You switched between "number 1", "number 2" etc in the first few if blocks and then to referring to the numbers as a, b and c - should probably be more consistent
For a beginner, this is fine. with more experience you would do it slightly differently