Python Forum
Print the Largest Odd Number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the Largest Odd Number
#1
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'))

while(TenNumbers):

    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')
Reply
#2
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
print(next(odd_numbers))
print(next(odd_numbers))
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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!

-Tim
Reply
#5
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
At some point you may wish to compare p to MaxValue and update MaxValue only if p is larger.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Linked List - Ordering by largest population with country name. PaleHorse 2 2,931 Jun-16-2020, 09:04 PM
Last Post: jefsummers
  How can I print the number of unique elements in a list? AnOddGirl 5 3,246 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  How to print a statement if a user's calculated number is between two floats Bruizeh 2 2,373 Feb-10-2019, 12:21 PM
Last Post: DeaD_EyE
  Print only number penoxcz 1 2,564 Nov-10-2017, 11:04 AM
Last Post: metulburr
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,763 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020