A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 one year ago
I apologize for the duplicate question. I closed the previous one by accident.
I've just started this course and I have the book.
I did the finger exercise after section 2.2 and am quite pleased that it worked.
The finger exercise after section 2.4 seems to be solvable by the same method, but it becomes tedious to enter and compare 10 variables. I wanted to do it with only 2 variables and a loop. I was able to, but only by setting the initial value of one variable to an arbitrary low negative value. This seems a bit of a cheat. Is there a more elegant way to do this or is this an acceptable technique?

print 'Given ten integers, this program will tell the highest odd.'
count = 0
y = 999999999999
for count in range (0, 10):
x = int(raw_input('Enter an integer.'))
if x>=y and (abs(x)%2)==1:
y = x
count = count + 1
if y ==999999999999:
print 'All of the inputs are even.'
else:
print y, 'is the greatest odd.'
 one year ago
I apologize for the duplicate question. I closed the previous one by accident. I've just started this course and I have the book. I did the finger exercise after section 2.2 and am quite pleased that it worked. The finger exercise after section 2.4 seems to be solvable by the same method, but it becomes tedious to enter and compare 10 variables. I wanted to do it with only 2 variables and a loop. I was able to, but only by setting the initial value of one variable to an arbitrary low negative value. This seems a bit of a cheat. Is there a more elegant way to do this or is this an acceptable technique?  print 'Given ten integers, this program will tell the highest odd.' count = 0 y = 999999999999 for count in range (0, 10): x = int(raw_input('Enter an integer.')) if x>=y and (abs(x)%2)==1: y = x count = count + 1 if y ==999999999999: print 'All of the inputs are even.' else: print y, 'is the greatest odd.'

This Question is Closed

khenduuur
 one year ago
Best ResponseYou've already chosen the best response.0If you want, you can set y == 0 and build on to your if statement a bit: for count in range (0, 10): x = int(raw_input('Enter an integer.')) > if y==0 and (abs(x)%2)==1: y = x > if x>=y and (abs(x)%2)==1: > y = x y = x count = count + 1 I think that will set y to the first odd number entered and the (x)%2==1 check makes sure user can't change it back to 0. I don't know if it's any more elegant but it does get rid of all those 9's. P.S. I'm sorry if this notifies you each time someone answers your question, I kept making spelling errors and then having to delete and retype.

bwCA
 one year ago
Best ResponseYou've already chosen the best response.1it is not a cheat if it works (or even if it doesn't). use a list. put all the odd integers in the list. then use the max() function to find the largest. http://docs.python.org/2.7/tutorial/introduction.html#lists http://docs.python.org/2.7/library/stdtypes.html#sequencetypesstrunicodelisttuplebytearraybufferxrange http://docs.python.org/2.7/library/functions.html#max or change your logic a bit. http://dpaste.com/954812/

slotema
 one year ago
Best ResponseYou've already chosen the best response.0The `count = count + 1` line in your code is not necessary. `range(10)` produces a list with 1, 2, 3, ..., 10 and at the end of the loop, it will automatically advance to the next item in the list, ignoring the value you just updated it to.

RCMorea
 one year ago
Best ResponseYou've already chosen the best response.0Thanks to all three of you! My program to sort 10 inputs is now shorter than the original one to sort 3 inputs. I find this very cool. I appreciate your comments. I will try to be useful to some other people as we go along.
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.