0

I am writing a program that asks a user to input 10 integers, then points to the largest odd integer among them. If no odd numbers are entered then a message should be printed to that effect.

What I have already is below:

a = int(input("Enter an integer: "))
b = int(input("Enter an integer: "))
c = int(input("Enter an integer: "))
d = int(input("Enter an integer: "))
e = int(input("Enter an integer: "))
f = int(input("Enter an integer: "))
g = int(input("Enter an integer: "))
h = int(input("Enter an integer: "))
i = int(input("Enter an integer: "))
j = int(input("Enter an integer: "))

numset = [a, b, c, d, e, f, g, h, i, j]
oddset = []
if (a%2, b%2, c%2, d%2, e%2, f%2, g%2, h%2, i%2, j%2) == (0, 0, 0, 0, 0, 0, 0, 0, 0, 0):
    print("No odd number was entered.")
else:
    for x in numset:
        if x%2 == 1:
            oddset.append(int(x)) 
    print(oddset)

I was able to identify all entered odd integers, but I'm stumped when it comes to picking out the largest from my list of odd numbers I called oddset. It may be an easy addition, but I am new to python so all help is appreciated. Also, if there are alternative ways of writing my code that are more concise, I'd love to see those too.

Matthias
  • 4,481
  • 12
  • 45
  • 84
Robert Forwerck
  • 31
  • 1
  • 2
  • 5
  • 1
    That is a list not a set. See https://stackoverflow.com/questions/6193498/pythonic-way-to-find-maximum-value-and-its-index-in-a-list#6194580 – Martin Beckett Sep 10 '18 at 16:38
  • Not by a computer but does `print(max(oddset))` work? – vash_the_stampede Sep 10 '18 at 16:39
  • 2
    Python comes with a function for that, but if this is really stumping you, then you should figure out how to write it manually first before using the function. You're going to have to write much harder things without a built-in function in the future. – user2357112 Sep 10 '18 at 16:39
  • @martinbeckett I think he is aware he says ‘my list named oddset’ I think he is just using set in his naming for his list not claiming it’s a actually set – vash_the_stampede Sep 10 '18 at 16:40
  • 1
    I would agree with the comments above in writing it from scratch, then looking at what the Python Standard Library has for [built-in functionality](https://docs.python.org/3/library/functions.html). Understand what is going on under the hood, but also leveraging what has already been built. – Erik Sep 10 '18 at 16:47
  • @vash_the_stampede - I wasn't being pedantic - but if the OP was searching for "how to find the max of a set" in python they would get the wrong answer. – Martin Beckett Sep 10 '18 at 16:51
  • vash_the_stampede: thank you! max(oddset) worked! I new about the max function but did not think once to use it here. – Robert Forwerck Sep 10 '18 at 16:51
  • @MartinBeckett Sets and lists are both iterables in Python, so the behavior would be the same. – Erik Sep 10 '18 at 16:54
  • @MartinBeckett No no I wasn't thinking that about you, nor was I trying to be condescending. I just was trying to clear up that it appeared to me OP was aware this was not a set. I understand the point you are making though, and it is a valid point. – vash_the_stampede Sep 10 '18 at 17:03

1 Answers1

1

You can simplify this. Create a list of odds, then you can use it for checking if it is empty or not as well as finding its max:

numset = [int(input("Enter an integer: ")) for _ in range(10)]  # list
# or
numset = {int(input("Enter an integer: ")) for _ in range(10)}  # set
# or most space efficient, not storing non-odds at all
numset = (int(input("Enter an integer: ")) for _ in range(10))  # lazy iterator

odds = [n for n in numset if n % 2]
try:
    print(max(odds))
except ValueError:
    print("No odd number was entered.")
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • I'm new to some of the semantics used here but it surely looks simpler than my code. I'll need to play around with it a bit. Thank you for your help!!! – Robert Forwerck Sep 10 '18 at 16:59