Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Print the Largest Odd Number
Hi, I have a Python homework question; I am ask to write a program where it asks users to input 10 integers and print out the largest odd number from the user inputs. However, I am restrained to only using "if, else, elif", while loop and mathematical operators (if needed). I am not allow to use for loop and the range function.

Here is my code that I have written up so far. I'm not sure if this is correct or if its the right approach. I tried to compile this code and I get an error message. Can anyone tell me what is wrong with my code? Is my thought process correct for answering this homework question? any help would be greatly appreciated! Thank You!

x = 0

TenNumbers = int(input('Enter 10 integers'))


    if (TenNumbers % 2 = 0):

        print(int(input('This is a even number, please enter another number')))

    else (TenNumbers % 2 != 0 and TenNumbers > x):

        print('This is the largest odd number')
The error you have is that comparing two values uses a double equals (==) to differentiate it from an assignment (=).

Your code isn't going to work though. Your input is before the loop. So it will be asked once and then the loop will keep going forever. Sort of. Your while loop condition tests the number entered, which is true as long as it isn't 0. So if they enter 0, the loop doesn't execute at all. You are not keeping track of how many times you ask the question, and you need to ask it exactly 10 times. And your maximum detector is inside your loop, will trigger for any value over 0, so every positive odd number will be hailed as the largest one.

What you need to do is:
  • Initialize the number of questions asked to 0.
  • Initialize the maximum to a large negative number.
  • Start a loop until you have asked 10 questions.
  • Ask for a number, and increase the number of questions asked by 1.
  • If the number is odd and greater than the maximum, set the maximum to that number.
  • End the loop
  • Print the maximum.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Are you allowed to use a generator?

sample = [44, 3, 25, 28, 65, 69, 98, 1, 29, 33, -77]
odd_numbers = (n for n in sample if n % 2 != 0)
The benefit is, that you get only odd numbers and you can take the first number for maximum_value.
Then you use odd_numbers in a for loop and assign maximum_value, if the current value is bigger.
You can do this also for minimum in the same function.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Hi Ichabod801,

thank you for your reply and your suggestion. However, I tried the what you suggested and it still doesn't work. Here is my code:

Count = 0
MaxValue = -99999999
while(Count != 0):
    p = int(input('Enter a odd number: '))
    if (p % 2 == 0):
        int(input('this is an even number, please enter another number'))
    elif (p % 2 != 0):
        count = count +1
        p = MaxValue
print('this is the largest odd number')

What is still wrong with my code? Any help or suggestions will be much appreciated. thanks!

Your while condition is false to start with, because Count starts at 0. (note that count and Count are two different variables, pick one and use it consistently). You want to loop while Count < 10, so the loop stops after 10 questions. Note that line 9 is backwards. You want to assign p to MaxValue, not MaxValue to p.

As it is now, your code goes until it gets 10 odd numbers, ignoring even numbers. That wasn't quite how you stated the problem initially. If that is what the problem requires, all is well. Otherwise, move line 9 down one line, and unindent it once.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

At some point you may wish to compare p to MaxValue and update MaxValue only if p is larger.
ichabod801 likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to print a statement if a user's calculated number is between two floats Bruizeh 2 350 Feb-10-2019, 12:21 PM
Last Post: DeaD_EyE
  Print only number penoxcz 1 901 Nov-10-2017, 11:04 AM
Last Post: metulburr
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 3,876 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

Users browsing this thread: 1 Guest(s)