我试图用Python编写一个简单的程序来计算x,y,z值中最大的奇数.如何为用户提供选择x,y和z值的选项?
所以程序会询问x,y和z是什么,然后说"x,y,z是最大的奇数"或者数字都是偶数.
我到目前为止的内容如下.这至少是一个不错的开始吗?
# This program exmamines variables x, y, and z
# and prints the largest odd number among them
if x%2 !== 0 and x > y and y > z:
print 'x is the largest odd among x, y, and z'
elif y%2 !== 0 and y > z and z > x:
print 'y is the largest odd among x, y, and z'
elif z%2 !== 0 and z > y and y > x:
print 'z is the largest odd among x, y, and z'
elif x%2 == 0 or y%2 == 0 or z%2 == 0:
print 'even'
Run Code Online (Sandbox Code Playgroud)
有了thkang帖子,我现在有:
# This program exmamines variables x, y, and z
# and prints the largest odd number among them
if x%2 !== 0:
if y%2 !== 0:
if z%2 !== 0:
if x > y and x > z: #x is the biggest odd
elif y > z and y > x: #y is the biggest odd
elif z > x and z > y: #z is the biggest odd
else: #z is even
if x > y: #x is the biggest odd
else: #y is the biggest odd
else: #y is even
if z%2 != 0: #z is odd
if x > z: #x is the biggest odd
else: #z is the biggest odd
else: #y,z are even and x is the biggest odd
else: #x is even
if y%2 != 0 and z%2 != 0; #y,z is odd
if y > z: #y is the biggest odd
else: #z is the biggest odd
else: #x and y is even
if z%2 != 0: #z is the biggest odd
Run Code Online (Sandbox Code Playgroud)
避免使用if-stmts以找到最大值.使用python内置max.使用任一发生器或filter仅查找奇数.
使用这样的内置函数更安全/更可靠,因为组合它们更简单,代码经过了充分测试,代码主要在C中执行(而不是多字节代码指令).
def find_largest_odd(*args):
return max(arg for arg in args if arg & 1)
Run Code Online (Sandbox Code Playgroud)
要么:
def find_largest_odd(*args):
return max(filter(lambda x: x & 1, args))
Run Code Online (Sandbox Code Playgroud)
>>> def find_largest_odd(*args):
... return max(arg for arg in args if arg & 1)
...
>>> print find_largest_odd(1, 3, 5, 7)
7
>>> print find_largest_odd(1, 2, 4, 6)
1
Run Code Online (Sandbox Code Playgroud)
和:
>>> def find_largest_odd(*args):
... return max(filter(lambda x: x & 1, args))
>>> print find_largest_odd(1, 3, 5, 7)
7
>>> print find_largest_odd(1, 2, 4, 6)
1
Run Code Online (Sandbox Code Playgroud)
如果您传递一个空序列或仅提供偶数,您将获得ValueError:
>>> find_largest_odd(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in find_largest_odd
ValueError: max() arg is an empty sequence
Run Code Online (Sandbox Code Playgroud)
如果有人正在寻找使用条件语句的简单解决方案
x,y,z = 4,1,6
largest = None
if x%2:
largest = x
if y%2:
if y > largest:
largest = y
if z%2:
if z > largest:
largest = z
if largest:
print "largest number is" largest
else
print "there are no odd numbers"
Run Code Online (Sandbox Code Playgroud)
最好先过滤数字,然后对它们进行排序。
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 number
elif sorted_odd_nums[0][0] == 1:
# y is the biggest odd number
elif sorted_odd_nums[0][0] == 2:
# z is the biggest odd number
Run Code Online (Sandbox Code Playgroud)
enumerate(numbers)返回一个(index, item)对的序列。由于原始列表是,所以即使在过滤和排序之后,[x, y, z]我们也可以跟踪x, 。yz
(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: '))
Run Code Online (Sandbox Code Playgroud)
你必须嵌套 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
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10499 次 |
| 最近记录: |