-2

I just started learning python. I have some experience with C++ from school. The problem is to write code that prints the largest odd number from user input and to print relevant feedback if there isn't any odd number. What is wrong with this code and are there better ways to solve this problem?

#To print the largest odd number 

x = input ("Enter first number: ")
y = input ("Enter second number: ")
z = input ("Enter third number: ")

if x % 2 == 0 and y % 2 == 0 and z % 2 == 0:
    print ("There are no odd numbers")

    if x % 2 != 0 and x > y and x > z:
        print (x, " is the largest odd number")

    if y % 2 != 0 and y > x and y > z:
        print (y, " is the largest odd number")

    if z % 2 != 0 and z > x and z > y:
        print (z, " is the largest odd number")

elif x == y == z:
    print ("All the numbers have the same value")    
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 3
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Aran-Fey Apr 10 '19 at 20:52
  • It is possible for the largest odd number not to be the largest number out of x, y, and z. For example, consider the case where x = 8, y = 5, and z = 3. – Jack Moody Apr 10 '19 at 20:55
  • 3
    The middle bunch of `if` statements can never be true, as they all start off with `if x % 2 != 0`, but you're already inside an `if x % 2 == 0` context. – John Gordon Apr 10 '19 at 20:56

3 Answers3

1

Maybe the logic becomes easier if you make it into a small list and sort it:

x = input ("Enter first number: ")
y = input ("Enter second number: ")
z = input ("Enter third number: ")

odds = sorted([ i for i in [x,y,z] if int(i)%2 ],reverse=True)
if not odds:
    print("No odd number")
elif odds.count(odds[0]) == len(odds):
    print("All odd numbers are equal")
else:
    print(f"{odds[0]} is the largest odd number")
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
0

2 things: 1. Convert data. 2. Your code isn't formatted correctly, by the looks of it.

For 1:

x = int(input(("Enter first number: ")) #Do this for y and z

For 2- your code will never return the largest odd number if the largest odd number is smaller than the largest even number. For example [20, 9, 5]. To fix that:

#Create a list to work with
num_li = [x,y,z]

#Get all the odd numbers    
num_li = [i for i in num_li if i%2!=0]

#If no odd numbers
if len(num_li) == 0:
    print('No odds')
#Print the largest odd number
else:
    num_li.sort(reverse = True)
    print('The largest odd number is: ' + str(num_li[0])) 
0

There are two problems with the current version of the code:

1)TypeError - You are receiving strings as inputs and treating them like integers

2)Logical error - your conditions do not cover all cases.

I rewrote the code. strings are converted to int and all cases are covered.

Working example:

x = int(input ("Enter first number: "))
y = int(input ("Enter second number: "))
z = int(input ("Enter third number: "))

numbers = [x, y, z]
odd_numbers = []
for number in numbers: // loop through the numbers and create an odd number list
  if number % 2 != 0:
    odd_numbers.append(number)

if odd_numbers: //print out the largest number in the list using max()
  print(max(odd_numbers))
else:
  print("No odd numbers")// if the list is empty this will be printed
danny bee
  • 840
  • 6
  • 19