我正在尝试用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'有了这个帖子,我现在有了:
# 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发布于 2020-12-25 14:54:50
下面是我为Guttag的手指练习2编写的代码:
def is_odd(x):
"""returns True if x is odd else returns False"""
if x % 2 != 0:
return(True)
else:
return(False)
def is_even(x):
"""returns True if x is even else returns False"""
if x % 2 == 0:
return(True)
else:
return(False)
def largest_odd(x, y, z):
"""Returns the largest odd among the three given numbers"""
if is_odd(x) and is_odd(y) and is_odd(z):
return(max(x, y, z))
elif is_odd(x) and is_odd(y) and is_even(z):
return(max(x, y))
elif is_odd(x) and is_even(y) and is_odd(z):
return(max(x, z))
elif is_even(x) and is_odd(y) and is_odd(z):
return(max(y, z))
elif is_odd(x) and is_even(y) and is_even(z):
return(x)
elif is_even(x) and is_odd(y) and is_even(z):
return(y)
elif is_even(x) and is_even(y) and is_odd(z):
return(z)
else:
return("There is no odd number in the input")https://stackoverflow.com/questions/15732805
复制相似问题