-1

I am working on an assignment and am absolutely stuck. I've been trying to figure it out for days but I'm a beginner and it feels like im just spinning my tires.

For the assignment I have to import a random number, and ask the user whether it is odd or even. Then based on the answer I have to have the program return whether it is correct or incorrect. However with this code I have pasted below I can't get it to execute my while loops at all.

def assign2PartA():
  import random
  strNumber = random.randrange(1,50)
  answer = raw_input("Is '" +  str(strNumber) +  "' Odd or Even?")  
  while strNumber % 2 == 0:
    if answer is "odd":
      return "incorrect"
    else:
      return "correct"
  while strNumber % 2 != 0:
    if answer is "odd":
      return "correct"
    else:
      return "incorrect"

Any help at all would be greatly appreciated. I just need some pointers to get me headed in the right direction as I have spent hours literally getting nowhere.

T Bev
  • 9
  • are you actually calling your function assign2PartA() after you define it? – Julien Oct 22 '15 at 01:17
  • 1
    I won't tell you the answer. This is supposed to be an assignment. A hint: Think about your control flow. You're using while-loops where they make no much sense. Try with if-statements. – santiagobasulto Oct 22 '15 at 01:17

2 Answers2

0

You don't need a loop, just a simple if..elif..else. Also, is checks for identity. You need to check for equality. You can use lower() to make the user's answer case-insensitive. Put the import statement at the top of your code. Lastly, the standard indentation is four spaces.

import random

def assign2PartA():
    strNumber = random.randrange(1,50)
    answer = raw_input("Is '" +  str(strNumber) +  "' Odd or Even?").lower()
    if (strNumber%2 and answer == 'odd') or (not strNumber%2 and answer == 'even'):
        return 'correct'
    else:
        return 'incorrect'

You could also store your answers in a list if you don't like that long if statement:

import random

def assign2PartA():
    strNumber = random.randrange(1,50) % 2
    answer = raw_input("Is '" +  str(strNumber) +  "' Odd or Even?").lower()
    answers = ['even', 'odd']
    if strNumber == answers[strNumber]:
        return 'correct'
    else:
        return 'incorrect'
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • @Snark - [Wrong.](http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce) – TigerhawkT3 Oct 22 '15 at 01:25
0

You were very close actually:

def assign2PartA():
  import random
  strNumber = random.randrange(1,50)
  answer = raw_input("Is '" +  str(strNumber) +  "' Odd or Even?").lower()
  while strNumber % 2 == 0:
    if answer == "odd":
      return "incorrect1"
    else:
      return "correct1"
  while strNumber % 2 != 0:
    if answer == "odd":
      return "correct"
    else:
      return "incorrect"

Two things I changed, one was your raw_input(), I added the lower. The other instead of is you need ==. is checks for object identity, while == check for equality.

Make sure you call the function at the end of course assign2PartA()

Leb
  • 15,483
  • 10
  • 56
  • 75
  • Using a `while` that will never loop rather than an `if` is poor practice. – TigerhawkT3 Oct 22 '15 at 01:22
  • @TigerhawkT3, I agree but I didn't want to recreate his homework solution. This answer was only to help show what was wrong with his code. – Leb Oct 22 '15 at 01:24
  • @Snark - [Wrong.](http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce) – TigerhawkT3 Oct 22 '15 at 01:25
  • @Snark no, it checks for identity, if the `str` happens to point to the same identity only then it's the same as equality. – Leb Oct 22 '15 at 01:26