Python Challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python Challenge

Can anyone make this code in Python looks more simple? The goal is to have the biggest odd number and if there is not an odd number, print out “All even numbers”. note: I would like to see if anyone can make it shorter and cleaner ;) https://code.sololearn.com/cSgYhj95h6Wa/?ref=app

16th Feb 2019, 5:22 PM
Ivan
16 Answers
+ 5
try: print(max([n for n in [int(m) for m in input().split()] if n%2])) except ValueError: print('all even')
16th Feb 2019, 7:16 PM
Anna
Anna - avatar
+ 6
# input like: 6 5 3 5 7 8 11 2 numbers = [int(n) for n in input().split()] odds = [n for n in numbers if n%2] if odds: print(max(odds)) else: print(*numbers)
16th Feb 2019, 6:12 PM
HonFu
HonFu - avatar
+ 6
l = sorted(l) # l = list with numbers while l: b = l.pop() if b%2: break print((False if b%2==0 else b) or 'all even')
16th Feb 2019, 6:34 PM
Anna
Anna - avatar
+ 6
Diego Interesting... If you remove or ["All even"] and enter only even numbers, it will raise a ValueError because you're trying to use max() on an empty list. That's why I used try/except. Seems like A or B is so powerful that it doesn't even care if A raises an exception. It'll just return B 🤓
17th Feb 2019, 5:10 AM
Anna
Anna - avatar
+ 5
Here's another method, it's a bit brutal try: print(max([n for n in l if n%2])) except ValueError: print('all even')
16th Feb 2019, 6:51 PM
Anna
Anna - avatar
+ 5
print(max([n for n in [int(m) for m in input().split()] if n%2] or ["All even"]))
17th Feb 2019, 12:00 AM
Diego
Diego - avatar
+ 3
l is the list containing the numbers. It's the same list that is called "numbers" in HonFu 's code
16th Feb 2019, 6:45 PM
Anna
Anna - avatar
+ 2
You want to get the biggest odd number. The code is looking for Odd only but if all number are even then it prints out all even. Even if the even number is bigger it is looking for the biggest odd number. HonFu
16th Feb 2019, 5:50 PM
Ivan
+ 2
You could switch Anna's first line to mine and write sorted before the [. Then you have my input pattern with Annas (by the way elegant) solution.
16th Feb 2019, 6:50 PM
HonFu
HonFu - avatar
+ 2
I heard people call this 'pythonic': Easier to ask for forgiveness than to ask for permission.
16th Feb 2019, 6:52 PM
HonFu
HonFu - avatar
+ 2
Data Breach , apparently you are not very used to analize code. Its something you learn with practice: you "run" the program in your mind and understand what it is doing. since you could not run anything from Anna , try to insert this lines of code at the beginning of her programs, as suggested by HonFu : # input like: 6 5 3 5 7 8 11 2 l = [int(n) for n in input().split()] may i also suggest that you make the Python course here in Sololearn? it will help you with this questions about the language statements.
16th Feb 2019, 7:19 PM
Edward
Edward - avatar
+ 1
Data Breach How is that meant? If the biggest odd number is not the biggest, take the biggest even one? I'm confused...
16th Feb 2019, 5:47 PM
HonFu
HonFu - avatar
+ 1
Aye! It works with integers, you have to have the program print out a string that says is the biggest odd number and all numbers are even. It is a simple code but it took me a whole week to understand :) HonFu
16th Feb 2019, 6:33 PM
Ivan
+ 1
Anna I ran your code and it recieved a traceback. Name ‘l’ is not defined? Is there something I am missing?
16th Feb 2019, 6:43 PM
Ivan
+ 1
The brutal code looks pleasing to me, I am trying to add an input to it but ‘l’ and ‘n’ both represent numbers. I am trying HonFu ‘s sorted and split input list with this try and except, it’s not working for me. How could I set up the input variables?
16th Feb 2019, 7:12 PM
Ivan
+ 1
Edward python is new to me ;) so I am just getting used to it and it is a challenge for others.
16th Feb 2019, 7:27 PM
Ivan