Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Python even-odd sorting list


  • Please log in to reply

#1
selfmade_android

selfmade_android

    New Member

  • Member
  • Pip
  • 1 posts
I need help with my program. I am getting a message that says: TypeError: unsupported operand type(s) for /: 'unicode' and 'int'
here is my program:
#Sorts a specified list of numbers into even and odd lists
ans = raw_input("Create a list of numbers seperated by commas to sort them into even and odd lists.")
ev = []
od = []
for i in [ans]:
if '.' in (i/2):
od.append(i)
else:
ev.append(i)
print("The even numbers are:")
print(ev)
print("The odd numbers are:")
print(od)
  • 0

Advertisements


#2
risingphoenix1985

risingphoenix1985

    Member

  • Member
  • PipPipPip
  • 131 posts
Hello,

Just putting your code in the code tags to make it easier to read.

#Sorts a specified list of numbers into even and odd lists
ans = raw_input("Create a list of numbers seperated by commas to sort them into even and odd lists.")
ev = []
od = []
for i in [ans]:
if '.' in (i/2):
    od.append(i)
else:
    ev.append(i)
print("The even numbers are:")
print(ev)
print("The odd numbers are:")
print(od)

I wouldn't use the "/" symbol, Python modulus would do the trick here. You also have a problem with your types in that you are performing an int function on a list class which I don't believe are compatible.

You may want to consider type casting the user input to take int's or floats.
  • 0

#3
risingphoenix1985

risingphoenix1985

    Member

  • Member
  • PipPipPip
  • 131 posts
Here is how I would have done it...

s = (raw_input("please enter numbers separated by , "))
s1 = s.split(",")
even = []
odd = []
for i in s1:
    num = long(i)
    if num % 2 !=1:
        even.append(int(num))
    else:
        odd.append(int(num))
print "even number set ...", even
print "odd number set...", odd

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP