Print the Largest Odd Number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Print the Largest Odd Number (/thread-19860.html) |
Print the Largest Odd Number - Than999 - Jul-17-2019 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') RE: Print the Largest Odd Number - ichabod801 - Jul-17-2019 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:
RE: Print the Largest Odd Number - DeaD_EyE - Jul-17-2019 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. RE: Print the Largest Odd Number - Than999 - Jul-24-2019 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 RE: Print the Largest Odd Number - ichabod801 - Jul-24-2019 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. RE: Print the Largest Odd Number - jefsummers - Jul-24-2019 At some point you may wish to compare p to MaxValue and update MaxValue only if p is larger. |