I have a question about While loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imish06
    New Member
    • Oct 2008
    • 2

    I have a question about While loops

    This is the question:

    "The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd number in its respective list. Print the lists of even and odd numbers at the end of the program. You must use a while loop."

    This is my code and algorythm:
    Code:
    #create empty lists to hold even and odd numbers
    even = ""
    odd = ""
    #create a message giving the directions
    mess = "Enter a non-negative number to add to the lists or a negative number to stop"
    #get input from the user
    val = raw_input(mess)
    #while input does not equal sentinal
    while (val > 0):
        #process the data using if statements
        if ((val%2) == 0):
            even = even + val
        else:
            odd = odd + val
        #get next value
        val = raw_input(mess)
    #print lists
    print even
    print odd
    I keep getting an error message when I run the program. Does anybody know what is wrong?
    Last edited by numberwhun; Oct 23 '08, 05:56 PM. Reason: Please use code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by imish06
    This is the question:

    "The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd number in its respective list. Print the lists of even and odd numbers at the end of the program. You must use a while loop."

    This is my code and algorythm:
    Code:
    #create empty lists to hold even and odd numbers
    even = ""
    odd = ""
    #create a message giving the directions
    mess = "Enter a non-negative number to add to the lists or a negative number to stop"
    #get input from the user
    val = raw_input(mess)
    #while input does not equal sentinal
    while (val > 0):
        #process the data using if statements
        if ((val%2) == 0):
            even = even + val
        else:
            odd = odd + val
        #get next value
        val = raw_input(mess)
    #print lists
    print even
    print odd
    I keep getting an error message when I run the program. Does anybody know what is wrong?
    raw_input() returns a string. You must convert the string to an integer. You mentioned lists. You are initializing two strings, but you should initialize two lists.

    Code:
    aList = []
    To add list elements, use the list method append().

    Comment

    • imish06
      New Member
      • Oct 2008
      • 2

      #3
      Originally posted by bvdet
      raw_input() returns a string. You must convert the string to an integer. You mentioned lists. You are initializing two strings, but you should initialize two lists.

      Code:
      aList = []
      To add list elements, use the list method append().

      I tried that and it didn't work. I keep getting the same message: "not all arguments converted during string formatting."

      Code:
      #creat empty lists to hold even and odd numbers
      even = []
      odd = []
      #create a message giving the directions
      mess = "Enter a non-negative number to add to the lists or a negative number to stop"
      #get input from the user
      val = raw_input(mess)
      #while input does not equal sentinal
      while (x > 0):
          #process the data using if statements
          if(x%2 ==0):
              even.append(val)
          else:
              odd.append(val)
          #get next value
          val = raw_input(mess)
      #print lists
      print even
      print odd

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        You need to use input() (or int(raw_input() ) ), since raw_input gives you a string. The % operator, when applied to strings, is the string formatting operator, used like this:

        [code=python]
        str = '%d+%d=%d' % (1, 1, 2)
        print str #Prints 1+1=2
        [/code]

        Thus, Python thinks you're trying to format a string when you're really trying to use modulus on an integer.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          As I said:
          raw_input() returns a string. You must convert the string to an integer.

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            Are you familliar with different variable types in Python? raw_input returns a string. For example, let's say you enter 3. Then without converting it to an int, val will hold the string "3". Since it is a string, you will get very strange results if you treat it like a number. For example, val * 5 is "33333". Five threes. So you need to convert it to an integer with the int function:
            Code:
            val = int(raw_input(mess))
            Because "3" is not the same as 3.
            Hope this makes a whole lot of sense. :-) Good luck.

            Comment

            Working...