I need help with this problem: Write a program that examines three variables (x, y, and z) and prints the largest odd number among them. If none of them are odd, it should print a message to that effect. So far I came up with this:
http://dpaste.com/hold/1356403/
When I try to run it in the shell I get this error: Traceback (most recent call last):
File "/Volumes/NO NAME/FE2.2.py", line 4, in
raw_input takes in a string. You need to change it to an int.
Take a look at this code. ``` def odd_checker(x,y,z): if (x%2 == 0) and (y%2 == 0) and (z%2 ==0): return "All even" odds = [] if x%2 != 0: odds.append(x) if y%2 != 0: odds.append(y) if z%2 != 0: odds.append(z) odds.sort() print odds[::-1] ```
After fixing that, it still has issues. I did 12, 1, and 2 and it did not print 1.
Another way to do it is with a loop. Set a variable to 0. As a person types in numbers, evaluate them. If they are even, leave it as 0. If they are odd, see if they are > the variable. If so, set the variable to that. Down side is that it does not account for negatives. However, you could do an extra test that if it is 0 and an odd, it always replaces.
Depends on if you really need three variables or if you just need three numbers.
See, that does it in not many lines: ``` number = 0 for i in range(3): x = int(raw_input('Gimme a number! ')) if x%2 != 0 and number == 0: number = x elif x%2 != 0 and x > number: number = x if number == 0: print'None of them are odd' else: print number ```
Now, where both my solution and russelladams' one breaks down is if you still need to know about the x, y, and z for some reason. I don't use them. He sorts them into a form where you would not know which it was. @russelladams Did you mean print odds[::-1] or return odds[::-1] at the bottom? You return earlier, so it seems like you meant this to be called with the data and return the result.
@e.mccormick Yeah, I meant to return, but it doesn't seem like there's a way to edit my responses. Lol, so I just let it go.
Hehe. You edit by reposting, then deleting. But at this point it does not matter because they can read all this and see things.
or even ``` >>> def f(sequence): n = max(n for n in sequence if n % 2) return n >>> x, y, z = 12, 43, 13 >>> f((x, y, z)) 43 >>> ```
Ah, a comprehension type line. Very Pythonic.
``` >>> def is_odd(n): return bool(n % 2) >>> x, y, z = 12, 43, 13 >>> max(filter(is_odd, (x, y, z))) ```
Seems this question is from "Introduction to Computation and Programming Using Python" by John V. Guttag on page 16... so long before people know about loops, arrays, and so on. All the resources they may have to answer it are if/elif/else, raw_input, int, and print. That makes for a much longer answer.
@headbuffalo you might use x = int(raw_input('x =')) y = int(raw_input('y =')) z = int(raw_input('z =')) to convert string type into integer type. then you code seem not exhaust all cases. for example the cases that there is only one odd number among x, y, z
I don't understand your code that you have now but here is a way I came up with (also attached runnable source code file) @echo off :: Take X Y and Z and output the largest odd number script by EMOTICON :D :: BTW I'm not really a computer programmer, but this does work :3 :: This program does not behave properly if spaces are entered in the prompts :: This program DOES behave properly by ignoring strings though :: Ask for user input. set /p x="What is X? " set /p y="What is Y? " set /p z="What is Z? " :: Check odd numbers set /a modulo = "%x% %% 2" if %modulo% == 0 ( set xo=false ) else ( set xo=true ) set /a modulo = "%y% %% 2" if %modulo% == 0 ( set yo=false ) else ( set yo=true ) set /a modulo = "%z% %% 2" if %modulo% == 0 ( set zo=false ) else ( set zo=true ) :: find largest number if %xo%==true echo "%x%">Num.temp if %yo%==true echo "%y%">Num.temp if %zo%==true echo "%z%">Num.temp if not exist Num.temp goto even findStr /R "[0-9]" "Num.temp">FindStr.temp del Num.temp for /F "tokens=*" %%a In (FindStr.temp) Do Call :odd %%a del FindStr.temp exit /B :odd set num=%1 set num=%num:"=% :: IF YOU WANT JUST A NUMBER AS OUTPUT, COMMENT THE "IF" COMMANDS AND UNCOMMENT "echo %num%" :: IF YOU WANT A USER FRIENDLY OUTPUT COMMENT "echo %num%" AND UNCOMMENT THE "IF" COMMANDS :: echo %num% if %x%==%num% (echo The largest odd number is X! [%num%]) if %y%==%num% (echo The largest odd number is Y! [%num%]) if %z%==%num% (echo The largest odd number is Z! [%num%]) pause exit /B :even echo All of these numbers are even and/or not numbers! pause exit /B just paste the code into notepad and save as a .bat file PS I am sorry if I suck at programming, I just don't understand it. This one took me two weeks to write (hope it works)
@Emoticon The MIT 6.00 class uses Python, not batch programming.
im so sorry :( i dont know python yet, i would like to know if the program does as intended though.
Join our real-time social learning platform and learn together with your friends!