Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择最大奇数_Python - Fatal编程技术网

Python 选择最大奇数

Python 选择最大奇数,python,Python,我试图用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

我试图用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'
通过thkang post,我现在有:

  # 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

最好先过滤这些数字,然后对它们进行排序

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
它的作用是:
枚举(数字)
返回一系列
(索引,项)
对。由于原始列表是
[x,y,z]
,因此我们可以跟踪
x
y
z
,即使在过滤和排序之后也是如此

(如果x[1]%2,则枚举(数字)中的x代表x)
如果给定元组中的第二项不是偶数,则过滤枚举上方的项

排序(…,key=lambda x:x[1],reverse=True)
使用第二个索引项(即原始编号)的值按降序对筛选项进行排序

用户输入 要从用户处读取,最简单的方法是使用
raw_input
(py2)/
input
(py3k)

仅使用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
...
请注意,
sorted
是一个
O(n log n)
操作,而
max
O(n)
。对于如此短的序列,速度差可能无关紧要,但最好使用最佳工具进行作业。

类似以下内容:

def get_max_odd(*lis):
    try:
         return sorted(i for i in lis if i%2)[-1] #IndexError if no odd item found
    except IndexError:    
         return "even"


In [8]: get_max_odd(1,2,3)
Out[8]: 3

In [9]: get_max_odd(2,4,6)
Out[9]: 'even'

In [10]: get_max_odd(2,5,6)
Out[10]: 5

In [11]: get_max_odd(2,4,6,8,9,10,20)
Out[11]: 9
方法 如果stmts查找最大值,请避免使用
。使用python内置的
max
。使用生成器或
过滤器
仅查找奇数

使用这样的内置代码更安全/更可靠,因为编写它们更简单,代码经过良好测试,并且代码主要以C语言执行(而不是多字节代码指令)

代码 或:

试验 以及:

如果传递空序列或仅提供偶数,则会得到
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
>>查找最大奇数(0)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第2行,查找奇数
ValueError:max()arg是一个空序列
工具书类

假设我们不打算使用一些花哨的东西:-)比如python中的max和sort函数……我认为可以肯定地说,“赋值”语句是公平的……毕竟我们必须赋值x、y和z

因此:我创建了自己的名为“最大”的变量,初始化为零。 我将变量赋值为x,y,z的第一个奇数(按该顺序查询)。 然后我简单地检查剩余的值,如果每个值都大于“最大值”,并且也是奇数,那么我将其设为“最大值”——并检查下一个值。 一旦所有的值都被选中,“最大”将保存最大奇数或零的值(这意味着我从未找到奇数,我打印了一条这样的消息)

这与从用户处读取任意数量(可能是10个)的值并在输入所有值后打印最大的奇数值的方法相同。只需将输入的每个值与当前的“最大”值进行对比即可


是的,您确实需要小心支持负奇数——您需要处理对第一个奇数的“最大”赋值,不管它是否大于零。

实际上,我更容易用更多的值来考虑这个问题,而不是只考虑3(x,y,z)假设我给你看一个单词列表,包括苹果、狗、香蕉、猫、猴子、斑马、大象……等等。假设有26个单词由变量a、b、c、d、e、f……x、y和z表示

如果你需要找到以小写字母开头的“最大单词”(按字典顺序),你不需要将每个单词与其他每个单词进行比较。我们的大脑通常不会将整个列表按顺序排序,然后从末尾进行选择……嗯……我的大脑也不会这样做


我只是从列表的最前面开始,一路读下去。当我第一次发现一个以小写字母开头的单词时……我会记住它,然后读到另一个小写字母——然后我检查哪个更大,只记住那个单词……我再也不必回去检查其他任何一个单词了。一次通过列表,我就不知道了e、 在上面的列表中…我的大脑立即丢弃“Zebra”,因为案例是错误的。

如果有人正在使用条件语句寻找简单的解决方案

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"

这是John V.Guttag的《使用Python的计算和编程入门》第2章的手指练习。这是MITx:6.00x《计算机科学和编程入门》的推荐文本。本书是为与Python 2.7一起使用而编写的

“编写一个程序,检查三个变量x、y和z,并打印其中最大的奇数。如果没有一个是奇数,它应该打印一条这样的消息。”

在这一点上,本书只介绍了变量赋值和条件分支程序以及打印函数。我知道我正在研究它。为此,我编写了以下代码:

if x%2 == 0:
    if y%2 == 0:
        if z%2 == 0:
            print "None of them are odd"
        else:
            print z
    elif y > z or z%2 == 0:
        print y
    else:
        print z
elif y%2 == 0:
    if z%2 == 0 or x > z:
        print x
    else:
        print z
elif z%2 == 0:
    if x > y:
        print x
    else:
        print y
else:
    if x > y and x > z:
        print x
    elif y > z:
        print y
    else:
        print z

它似乎适用于我尝试过的所有组合,但考虑到我在第三段中的观点,知道它是否可以缩短会很有用。我很感激有人问我这个问题,但通过提供这个答案,其他用户在搜索书中问题的答案时更可能会遇到它。

我正在工作通过Guttag(和Python2.7)的同一本书。其他人阅读这本书时可能会意识到,虽然我在解决方案中使用了一个列表(或对它们进行切片),但列表(或对它们进行切片)尚未被引入(我认为它工作得很好!?!)。如果您希望从列表的末尾而不是开始切片,则不需要颠倒列表顺序

首先,我创建了一个空列表(lst),但在使用它之前,我检查x、y和z是否都是空的
def find_largest_odd(*args):
    return max(filter(lambda x: x & 1, args))
>>> 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
>>> 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
>>> 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
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"
if x%2 == 0:
    if y%2 == 0:
        if z%2 == 0:
            print "None of them are odd"
        else:
            print z
    elif y > z or z%2 == 0:
        print y
    else:
        print z
elif y%2 == 0:
    if z%2 == 0 or x > z:
        print x
    else:
        print z
elif z%2 == 0:
    if x > y:
        print x
    else:
        print y
else:
    if x > y and x > z:
        print x
    elif y > z:
        print y
    else:
        print z
x,y,z = 111,45,67

lst = []
if x%2==0 and y%2==0 and z%2==0:
    print 'none of x,y or z is an odd number'
else:
    if x%2!=0:
        lst.append(x)
    if y%2!=0:
        lst.append(y)
    if z%2!=0:
        lst.append(z)
lst.sort(reverse = True)
if len(lst)!=0:
    print lst[:1]
x = 333312
y = 221569
z = 163678

if x%2 ==0 and y%2==0 and z%2==0:
    print "Only even numbers provided."

elif x%2==0:
    if y%2!=0 and z%2!=0:
        if y > z:
            print y
        else:
            print z
    else:
        if y%2!=0:
            print y
        if z%2!=0:
            print z


elif y%2==0:
    if x%2!=0 and z%2!=0:
        if x > z:
            print x
        else:
            print z
    else:
        if x%2!=0:
            print x
        if z%2!=0:
            print z



elif z%2==0:
    if x%2!=0 and y%2!=0:
                if x > y:
                    print x
                else:
                    print y
    else:
        if x%2!=0:
                print x
        if y%2!=0:
                print y

else:
    if x>y and x>z:
        print x
    elif y>z:
        print y   
    else:
        print z
x = 2
y =4
z=6

# Conditional program to print the smallest odd number

if x %2 == 1 or y% 2 == 1 or z% 2 == 1 : #checks if at least one is odd
    if x<y and x< z:  # checks if x is least
        print x
    elif y<z:         # x is not least,so proceeds to check if y is least
        print y
    else:
        print z       #prints z, the least number
else:
    print "None of the numbers are odd"
x, y, z = 5,7,7

if x%2 == 1 or y%2 == 1 or z%2 == 1: #at least one of the variables is odd
    if x%2 == 1:
        if y%2 == 1:
            if z%2 == 1: #x, y, and z are all odd
                if x >= y and x >= z:
                    print "x =", x, "is the largest odd number!"
                if y >= x and y >=z:
                    print "y =",y, "is the largest odd number!"
                if z >= x and z >= y:
                    print "z =", z, "is the largest odd number!"
            else: #z is even, but x and y are still odd
                if x >= y:
                    print "x =", x, "is the largest odd number!"
                if y >= x:
                    print "y =",y, "is the largest odd number!"
        elif z%2 == 1: #y is even, but x and z are odd
            if x >= z:
                print "x =", x, "is the largest odd number!"
            if z >= x:
                print "z =", z, "is the largest odd number!"
        else: #x is the only odd number
            print "x = ", x, "is the largest odd number!"
    if x%2 != 1 and y %2 == 1: #x is not odd but y is odd
    #could have done an elif here. But this makes the code easier to follow
        if z%2 == 1:#z is also odd
            if y >=z:
                print "y =",y, "is the largest odd number!"
            if z >= y:
                print "z =", z, "is the largest odd number!"
        else: #z is even. Hence, y is the only odd number
            print "y =", y, "is the largest odd number!"
    if x%2 != 1 and y%2 != 1 and z%2 == 1:#z is the only odd number
        print "z =", z, "is the largest odd number!"             
else:
    print "No odd number was input!"
import math



a=int(input("Enter first integer: ")) # Manual entering of ten integers#
b=int(input("Enter second integer: "))
c=int(input("Enter third integer: "))
d=int(input("Enter fourth integer: "))
e=int(input("Enter fifth integer: "))
f=int(input("Enter sixth integer: "))
g=int(input("Enter seventh integer: "))
h=int(input("Enter eighth integer: "))
i=int(input("Enter ninth integer: "))
j=int(input("Enter tenth integer: "))

master=[a,b,c,d,e,f,g,h,i,j]



def ifalleven(a): # Finding whether every integer in the list if even or not.#

    i=0
    list=[]
    while i<len(a):
        if a[i]%2==0:
            list.append(a[i])
        i=i+1
    return len(list)==len(a)



def findodd(a): # Finding the greatest odd integer on the list.#
    i=0
    list=[]
    while i<len(a):
        if a[i]%2!=0:
            list.append(a[i])
        i=i+1
    print (max(list))



def greatoddoreven(list): # Finding the greatest odd integer on the list or if none are odd, print a message to that effect.#
    if ifalleven(list)==True:
        print ('No odd numbers found!')
    else:
        findodd(list)



greatoddoreven(master)
x = int(input('Enter an integer: '))
y = int(input('Enter another integer: '))
z = int(input('Enter a final integer: '))

print('x is:', x)
print('y is:', y)
print('z is:', z)
print('\n')

if x % 2 == 1 and y % 2 == 1 and z % 2 == 1:
    print('First conditional triggered.')
    if x > y and x > z:
        print(x, 'is the largest odd number.')
    elif y > x and y > z:
        print(y, 'is the largest odd number.')
    elif z > x and z > y:
        print(z, 'is the largest odd number.')
    else:
        print(x, 'is the largest odd number.')
    # This else clause covers the case in which all variables are equal.
    # As such, it doesn't matter which variable you use to output the 
    # largest as all are the largest.
elif x % 2 == 1 and y % 2 == 1 and z % 2 == 0:
    print('Second conditional is triggered.')
    if x > y:
        print(x, 'is the largest odd number.')
    elif y > x:
        print(y, 'is the largest odd number.')
    else:
        print(x, 'is the largest odd number.')
elif x % 2 == 1 and y % 2 == 0 and z % 2 == 1:
    print('Third conditional is triggered.')
    if x > z:
        print(x, 'is the largest odd number.')
    elif z > x:
        print(z, 'is the largest odd number.')
    else:
        print(x, 'is the largest odd number.')
elif x % 2 == 1 and y % 2 == 0 and z % 2 == 0:
    print('Fourth conditional is triggered.')
    print(x, 'is the largest odd number.')
elif x % 2 == 0 and y % 2 == 1 and z % 2 == 1:
    print('Fifth conditional is triggered.')
    if y > z:
        print(y, 'is the largest odd number.')
    elif z > y:
        print(z, 'is the largest odd number.')
    else:
        print(y, 'is the largest odd number.')
elif x % 2 == 0 and y % 2 == 1 and z % 2 == 0:
    print('Sixth conditional is triggered.')
    print(y, 'is the largest odd number.')
elif x % 2 == 0 and y % 2 == 0 and z % 2 == 1:
    print('Seventh conditional is triggered.')
    print(z, 'is the largest odd number.')
else:
    print('Eight conditional is triggered.')
    print('There is no largest odd number as all numbers are even.')
x, y, z = 55, 90, 87

if x%2 == 1 and y%2 == 1 and z%2 == 1:
   if x > y and x > z:
        print (x)
   elif y > z and y >x:
        print (y)
   else:
        print (z)
else:   
    print ('none of them are odd')
num1 = 7
num2 = 16
num3 = 300
x1=0
x2=0
x3=0

if num1%2 != 0:
    x1=1
if num2%2 != 0:
    x2=1
if num3%2 != 0:
    x3=1

if (num1*x1 > num2*x2) and (num1*x1 > num3*x3):
   largest = num1
elif (num2*x2 > num1*x1) and (num2*x2 > num3*x3):
   largest = num2
elif (x3):
   largest = num3
else:
    print("no one is odd")
if(x1!=0 or x2!=0 or x3!=0):
    print("The largest odd number between",num1,",",num2,"and",num3,"is",largest)
 x, y, z = eval(input('Please enter the numbers for x, y, z: '))#allows you to enter three numbers each separated by a comma
print('x =', x)#Shows you what the value of x is
print('y =', y)#Shows you what the value of y is
print('z =', z)#Shows you what the value of z is
if x%2 != 0:#Checks to see if x is odd is true 
    if y%2 != 0:#Checks to see if y is odd is true
        if z%2 != 0:#Checks to see if z is odd is true
            if x > y and x > z:# Now in this situation since all the numbers are odd, all we have to do is compare them to see which is the largest in the following lines
                print('x is the largest odd number')
            elif y > z:    
                print('y is the largest odd number')
            else:    
                print('z is the largest odd number')
        elif x > y:# we refer back to the line if z%2 != 0 and in this new situation, it is false therefore z is not odd in this case and x and y are the only odd numbers here
                print('x is the largest odd number')  # we check to see if x is the largest odd number, if x is not largest odd number then proceed to the next line      
        else: 
                print('y is the largest odd number')  # here, y is the largest odd number                                                  
    elif z%2 != 0:# refer back to the sixth line at the beginning, if y%2 = 0.In this new situation, y is not odd so the only variables we're comparing now are x and z only
        if x > z:
            print('x is the largest odd number')
        else:
            print('z is the largest odd number')
    else:
            print('x is the largest odd number')# when both y and z are not odd, by default x is the largest odd number here
elif y%2 != 0:# Refer back to the fifth line, if x%2 != 0, in this new situation, the statement if x%2 != 0 is false therefore we proceed to checking if the next variable y is odd right here
            if z%2 != 0:#Since y is odd, check to see if z is odd,
                if y > z:#then if previous statement is true check to see now if y is bigger and print('y is the largest odd number') if so
                    print('y is the largest odd number')
                else:
                    print('z is the largest odd number')# here y is not largest odd number so by default z is the largest

            else:# this line occurs when z is not odd which pretty much leaves y to be the only odd number and is therefore largest odd number by default
                print('y is the largest odd number')
elif z%2 != 0:# this line occurs when both x and y is not odd which leaves z to be the largest odd number
            print('z is the largest odd number')
else:# this line occurs when all the numbers are not odd; they are even. Remember in this program, our main objective is to determine the largest number only among the odd numbers if there are any odd numbers at all, and if there are no odd numbers we show that there are none. (We do not take into account even numbers at all since that's not what the question is about) 
    print('There are no odd numbers')
x=input('x= ')
y=input('y= ')
z=input('z= ')
largest = None
if x%2 == 0 and y%2 == 0 and z%2 == 0:
    print('none of them is odd')
else:
    if x%2 != 0:
        largest = x
    if y%2 != 0:
        if y > largest:
            largest = y
    if z%2 != 0:
        if z > largest:
            largest = z
    print(largest)
    x = 4
    y = 7
    z = 7

    if x%2 and y%2 and z%2 == 1:
        if x > y and x > z:
            print x
        elif y > z:
            print y
        else:
            print z
    elif x%2 and y%2 == 1:
        if x > y:
            print x
        else:
            print y
    elif x%2 and z%2 == 1 :
        if x > z:
            print x
        else:
            print z
    elif y%2 and z%2 == 1:
        if y > z:
            print y
        else:
            print z
    elif x%2 == 1:
        print x
    elif y%2 == 1:
        print y
    elif z%2 == 1:
        print z
    else:
        print "there are no odd numbers"
def get_max_odd(x, y, z):
    odd_nums = []

    if x % 2 == 1:
        odd_nums.append(x)
    if y % 2 == 1:
        odd_nums.append(y)
    if z % 2 == 1:
        odd_nums.append(z)

    if odd_nums == []:
        return 'None of x, y, z are odd numbers'
    else:
        return max(odd_nums)

print(get_max_odd(120, 111, 23))
if x%2!=0 and y%2!=0 and z%2!=0 and x>y and x>z:
    print(x)
elif x%2!=0 and y%2!=0 and z%2!=0 and y>x and y>z:
    print(y)
elif x%2!=0 and y%2!=0 and z%2!=0 and z>x and z>y:
    print(z)
elif x%2==0 and y%2!=0 and z%2!=0 and y>z:
    print(y)
elif x%2==0 and y%2!=0 and z%2!=0 and z>y:
    print(z)
elif x%2!=0 and y%2!=0 and z%2==0 and y>x:
    print(y)
elif x%2!=0 and y%2!=0 and z%2==0 and x>y:
    print(x)
elif x%2!=0 and y%2==0 and z%2!=0 and x>z:
    print(x)
elif x%2!=0 and y%2==0 and z%2!=0 and z>x:
    print(z)
else:
    print('none of them are odd')
x = input("enter a number:")

y = input("enter another number: ")

z = input("enter another number: ")

x,y,z = int(x),int(y),int(z)


if x %2 !=0: # check whether x is odd

    if y%2 !=0: # if x is odd check y

        if z%2 != 0: # if y is odd then check z
            if x>z and x > y: # comparing the 3 numbers as all are odd to get largest
                print("x is largest")
            elif y>z:
                print("y is largest")
            else:
                print("z is largest")
        else: # if z is not an odd number then the above comparisons cannot be done #
    #so we placed this else if z is not odd then the below comparisons will be executed
            if x>z and x > y:
                print("x is largest")
            elif y>z:
                print("y is largest")
    else: # if y is also not an even and x is odd then x is largest irrespective of others
        print("x is largest")

elif y%2 != 0: # similar way if x is not odd then the above loop will not run so this gets executed when x is even

    if z%2 != 0:
        if y>z:
            print("y is largest")
    else:
        print("z is largest")
elif z%2 !=0:

    print("z is largest")
else:

    print("all the numbers are even")
def odd(x,y,z):
    l=[x,y,z]
    even=[]
    for e in l:
        if e%2!=0:
            even.append(e)
    even.sort()
    if len(even)==0:
        return(print('No odd numbers'))
    else:
        return(even[-1])

odd(1,2,3)
print('This program will find the largest odd number among the three entered.\nSo, let\'s start...\n')

x = int(input('Enter the 1st number: '))
y = int(input('Enter the 2nd number: '))
z = int(input('Enter the 3rd number: '))
strToPrint = ' is the largest odd number.'

if x==0 or y==0 or z==0:
    print('\nNo zeroes, please. Re-run the program.')
    exit()
if x % 2 == 0 and y % 2 == 0 and z % 2 == 0:
    print('\nAll numbers are even.')
elif x % 2 != 0 and y % 2 != 0 and z % 2 != 0:  # all numbers are odd; start analysis...
    if x > y and x > z:
        print(str(x) + strToPrint)
    if y > x and y > z:
        print(str(y) + strToPrint)
    if z > y and z > x:
        print(str(z) + strToPrint)
elif x % 2 != 0 and y % 2 == 0 and z % 2 == 0:  # only X is odd.
    print(str(x) + strToPrint)
elif y % 2 != 0 and x % 2 == 0 and z % 2 == 0:  # only Y is odd.
    print(str(y) + strToPrint)
elif z % 2 != 0 and x % 2 == 0 and y % 2 == 0:  # only Z is odd.
    print(str(z) + strToPrint)
elif x % 2 != 0 and y % 2 != 0 and z % 2 == 0:  # only X and Y are odd.
    if x > y:
        print(str(x) + strToPrint)
    else:
        print(str(y) + strToPrint)
elif x % 2 != 0 and y % 2 == 0 and z % 2 != 0:  # only X and Z are odd.
    if x > z:
        print(str(x) + strToPrint)
    else:
        print(str(z) + strToPrint)
elif x % 2 == 0 and y % 2 != 0 and z % 2 != 0:  # only Z and Y are odd.
    if y > z:
        print(str(y) + strToPrint)
    else:
        print(str(z) + strToPrint)
int_list = [100, 2, 64, 98, 89, 25, 70, 76, 23, 5]
i = len(int_list) - 1           # we'll start from the end of a sorted (!) list
int_list.sort()                 # sort the list first

# print(int_list)               # just to check that the list is indeed sorted. You can uncomment to print the sorted list, too.

while i >= 0:                   # prevent 'out of range' error
    if int_list[i] % 2 != 0:    # check if the list item is an odd number
        break                   # since the list is sorted, break at the first occurence of an odd number
    i -= 1                      # continue reading list items if odd number isn't found

print('The largest odd number is: ' + str(int_list[i]) + ' (item #' + str(i) + ' in the sorted list)')
x, y, z = 7, 6, 12
xo = 0
yo = 0
zo = 0

if x%2 == 0 and y%2 == 0 and z%2 == 0:
    print("None of the numbers are odd.")

if x%2 == 1:
    xo = x

if y%2 == 1:
    yo = y

if z%2 == 1:
    zo = z

if xo > yo and xo > zo:
    print(xo)

if yo > xo and yo > zo:
    print(yo)

if zo > xo and zo > yo:
    print(zo)
# edge cases 100, 2, 3 and 100,2,-3 and 2,2,2
x = 301
y = 2
z = 1

# what if the largest number is not odd? We need to discard any non-odd numbers

# if none of them are odd - go straight to else
if (x % 2 != 0) or (y % 2 != 0) or (z % 2 != 0):
    #codeblock if we have some odd numbers
    print("ok")
    
    # initialising the variable with one odd number, so we check each in turn
    if (x % 2 != 0):
        largest = x
    elif (y % 2 != 0):
        largest = y
    elif (z % 2 != 0):
        largest = z

    # here we check each against the largest
    # no need to check for x as we did already

    if (y % 2 != 0):
        if y > largest:
            largest = y
    
    if (z % 2 != 0):
        if z > largest:
            largest = z
    
    print("The largest odd number is:", largest)
    print("The numbers were", x, y, z)
        
else: 
    print("No odd number found")`
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")