patternpythonMinor
Printing inputted, largest, odd integer
Viewed 0 times
inputtedlargestprintingintegerodd
Problem
The program asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it prints a message to that effect.
q = int(raw_input("Please enter an integer:"))
r = int(raw_input("Please enter another integer:"))
s = int(raw_input("Please enter another integer:"))
t = int(raw_input("Please enter another integer:"))
u = int(raw_input("Please enter another integer:"))
v = int(raw_input("Please enter another integer:"))
w = int(raw_input("Please enter another integer:"))
x = int(raw_input("Please enter another integer:"))
y = int(raw_input("Please enter another integer:"))
z = int(raw_input("Please enter one last integer:"))
odd = []
if q%2 != 0:
odd += [q]
if r%2 != 0:
odd += [r]
if s%2 != 0:
odd += [s]
if t%2 != 0:
odd += [t]
if u%2 != 0:
odd += [u]
if v%2 != 0:
odd += [v]
if w%2 != 0:
odd += [w]
if x%2 != 0:
odd += [x]
if y%2 != 0:
odd += [y]
if z%2 != 0:
odd += [z]
if q%2 == 0 and r%2 == 0 and s%2 == 0 and t%2 == 0 and u%2 == 0 and v%2 == 0 and w%2 == 0 and x%2 == 0 and y%2 == 0 and z%2 == 0:
print "None of the values given are odd."
else:
print max(odd), "is the largest odd number."Solution
Use loops!
How to read this line:
This line means starting at 0 (the default starting point for xrange()), and ending at 10-1 (upper bound is not included), execute the following code with
The rest was made as similar to your code as possible, and so should be understandable.
odd_nums = []
for i in xrange(10):
value = int(raw_input('Enter an integer: '))
if value % 2 != 0:
odd_nums.append(value)
if len(odd_nums) != 0:
print max(odd_nums)
else:
print "No odd values"How to read this line:
for i in xrange(10):This line means starting at 0 (the default starting point for xrange()), and ending at 10-1 (upper bound is not included), execute the following code with
i set to the current iteration of the loop. This executes the code exactly 10 times (i = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9).The rest was made as similar to your code as possible, and so should be understandable.
Code Snippets
odd_nums = []
for i in xrange(10):
value = int(raw_input('Enter an integer: '))
if value % 2 != 0:
odd_nums.append(value)
if len(odd_nums) != 0:
print max(odd_nums)
else:
print "No odd values"for i in xrange(10):Context
StackExchange Code Review Q#57410, answer score: 4
Revisions (0)
No revisions yet.