patternpythonMajor
Asks the user to input 10 integers, and then prints the largest odd number
Viewed 0 times
oddthenumberuserprintsinputlargestthenandintegers
Problem
I have written a piece of python code in response to the following question and I need to know if it can be "tidied up" in any way. I am a beginner programmer and am starting a bachelor of computer science.
Question: Write a piece of code that asks the user to input 10 integers, and then prints the largest odd number that was entered. if no odd number was entered, it should print a message to that effect.
My solution:
Question: Write a piece of code that asks the user to input 10 integers, and then prints the largest odd number that was entered. if no odd number was entered, it should print a message to that effect.
My solution:
a = input('Enter a value: ')
b = input('Enter a value: ')
c = input('Enter a value: ')
d = input('Enter a value: ')
e = input('Enter a value: ')
f = input('Enter a value: ')
g = input('Enter a value: ')
h = input('Enter a value: ')
i = input('Enter a value: ')
j = input('Enter a value: ')
list1 = [a, b, c, d, e, f, g, h, i, j]
list2 = [] # used to sort the ODD values into
list3 = (a+b+c+d+e+f+g+h+i+j) # used this bc all 10 values could have used value'3'
# and had the total value become an EVEN value
if (list3 % 2 == 0): # does list 3 mod 2 have no remainder
if (a % 2 == 0): # and if so then by checking if 'a' has an EVEN value it rules out
# the possibility of all values having an ODD value entered
print('All declared variables have even values')
else:
for odd in list1: # my FOR loop to loop through and pick out the ODD values
if (odd % 2 == 1):# if each value tested has a remainder of one to mod 2
list2.append(odd) # then append that value into list 2
odd = str(max(list2)) # created the variable 'odd' for the highest ODD value from list 2 so i can concatenate it with a string.
print ('The largest ODD value is ' + odd)Solution
Here are a couple of places you might make your code more concise:
First, lines 2-11 take a lot of space, and you repeat these values again below when you assign list1. You might instead consider trying to combine these lines into one step. A list comprehension might allow you to perform these two actions in one step:
A second list comprehension might further narrow down your results by removing even values:
You could then see if your list contains any values. If it does not, no odd values were in your list. Otherwise, you could find the max of the values that remain in your list, which should all be odd:
In total, then, you might use the following as a starting point for refining your code:
First, lines 2-11 take a lot of space, and you repeat these values again below when you assign list1. You might instead consider trying to combine these lines into one step. A list comprehension might allow you to perform these two actions in one step:
>>> def my_solution():
numbers = [input('Enter a value: ') for i in range(10)]A second list comprehension might further narrow down your results by removing even values:
odds = [y for y in numbers if y % 2 != 0]You could then see if your list contains any values. If it does not, no odd values were in your list. Otherwise, you could find the max of the values that remain in your list, which should all be odd:
if odds:
return max(odds)
else:
return 'All declared variables have even values.'In total, then, you might use the following as a starting point for refining your code:
>>> def my_solution():
numbers = [input('Enter a value: ') for i in range(10)]
odds = [y for y in numbers if y % 2 != 0]
if odds:
return max(odds)
else:
return 'All declared variables have even values.'
>>> my_solution()
Enter a value: 10
Enter a value: 101
Enter a value: 48
Enter a value: 589
Enter a value: 96
Enter a value: 74
Enter a value: 945
Enter a value: 6
Enter a value: 3
Enter a value: 96
945Code Snippets
>>> def my_solution():
numbers = [input('Enter a value: ') for i in range(10)]odds = [y for y in numbers if y % 2 != 0]if odds:
return max(odds)
else:
return 'All declared variables have even values.'>>> def my_solution():
numbers = [input('Enter a value: ') for i in range(10)]
odds = [y for y in numbers if y % 2 != 0]
if odds:
return max(odds)
else:
return 'All declared variables have even values.'
>>> my_solution()
Enter a value: 10
Enter a value: 101
Enter a value: 48
Enter a value: 589
Enter a value: 96
Enter a value: 74
Enter a value: 945
Enter a value: 6
Enter a value: 3
Enter a value: 96
945Context
StackExchange Code Review Q#22784, answer score: 31
Revisions (0)
No revisions yet.