最好过滤数字,然后对它们进行排序。
numbers = [x, y, z]sorted_odd_nums = sorted((x for x in enumerate(numbers) if x[1]%2), key = lambda x:x[1], reverse=True)if not sorted_odd_nums: # all numbers were even and filtered out.elif sorted_odd_nums[0][0] == 0: # x is the biggest odd numberelif sorted_odd_nums[0][0] == 1: # y is the biggest odd numberelif sorted_odd_nums[0][0] == 2: # z is the biggest odd number
它能做什么:
enumerate(numbers)返回一个序列
(index, item)对。因为原来的名单是
[x, y,z],我们可以保持的轨道
x,
y,
z甚至筛选和排序后。
(x for x in enumerate(numbers) if x[1]%2)如果给定元组中的第二项不是偶数,则在枚举上方进行过滤。
sort( ... , key=lambda x:x[1], reverse=True)使用第二索引项目的值(原始编号)以降序对过滤的项目进行排序。
用户输入
要从用户那里读取信息,最简单的方法是使用
raw_input(py2)/
input(py3k)。
number = int(raw_input('enter a number: '))仅使用if语句
您将必须嵌套if语句。喜欢:
if x%2: # x is odd if y%2: # y is odd if z%2: #z is odd if x>y and x>z: #x is the biggest odd number elif y>z and y>x: #y is the biggest odd number elif z>x and z>y: #z is the biggest odd number else: #z is even if x>y: #x is the biggest odd number else: #y is the biggest odd number else: #y is even if z%2: #z is odd...



