+ 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
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')
+ 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)
+ 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')
+ 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 🤓
+ 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')
+ 5
print(max([n for n in [int(m) for m in input().split()] if n%2] or ["All even"]))
+ 3
l is the list containing the numbers. It's the same list that is called "numbers" in HonFu 's code
+ 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
+ 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.
+ 2
I heard people call this 'pythonic':
Easier to ask for forgiveness than to ask for permission.
+ 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.
+ 1
Data Breach
How is that meant?
If the biggest odd number is not the biggest, take the biggest even one?
I'm confused...
+ 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
+ 1
Anna
I ran your code and it recieved a traceback.
Name ‘l’ is not defined?
Is there something I am missing?
+ 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?