Feb-12-2018, 07:04 PM (This post was last modified: Feb-12-2018, 08:24 PM by Mekire. Edited 5 times in total.)

Hi,

I am learning python using John Guttag's book - "Introduction to Computation and Programming Using Python: With Application to Understanding Data (MIT Press) second edition".

There's a finger exercise in the book that states the following:

Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.

I cannot use a for loop or range function or arrays because I am supposed to write the solution using the keywords, functions and concepts that are discussed in the book just before this exercise. So that includes a while loop, if/elif/else, == != True/False and or etc. Basically nothing except the stuff I have used in the script below.

Here's the solution I have come up with:

Is there a better way to write this? Please let me know.

Note:- I am an office going fellow who gets only an hour or so a day (if possible) to work on this. I've been scratching my head to come up with the solutions since quite a while, and this is the best that I have done so far, so I have really reached my thinking limit with this one. :)

Post reverted. Please don't do that.

I am learning python using John Guttag's book - "Introduction to Computation and Programming Using Python: With Application to Understanding Data (MIT Press) second edition".

There's a finger exercise in the book that states the following:

Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.

I cannot use a for loop or range function or arrays because I am supposed to write the solution using the keywords, functions and concepts that are discussed in the book just before this exercise. So that includes a while loop, if/elif/else, == != True/False and or etc. Basically nothing except the stuff I have used in the script below.

Here's the solution I have come up with:

counter = 0 even_count = 0 biggest_odd = False while counter <= 10: counter = counter + 1 x = int(input("Enter a number: ")) if counter == 1 or (x % 2 != 0 and x > biggest_odd) : biggest_odd = x #The line below helped me debug the script. #print("biggest odd in the while loop is now " , biggest_odd) elif x % 2 == 0: even_count = even_count + 1 #if I dont do #if biggest_odd % 2 != 0, # the script says "No odd number entered" #if one of the numbers is a negative odd number #and the rest are zeros. if biggest_odd % 2 != 0: print (biggest_odd, " is the biggest odd") elif even_count == 10: print("No odd numbers entered")Problem is, if I don't do the "if biggest_odd % 2 != 0", the script says "No odd number entered" if, lets say, the first number entered is -3 or -1 (a negative odd number), and then for the rest of the 9 times, I enter a zero.

Is there a better way to write this? Please let me know.

Note:- I am an office going fellow who gets only an hour or so a day (if possible) to work on this. I've been scratching my head to come up with the solutions since quite a while, and this is the best that I have done so far, so I have really reached my thinking limit with this one. :)

**wrote Feb-12-2018, 08:24 PM:***Mekire*Post reverted. Please don't do that.