-2

This code usually puts the smallest number in the inputed list in the front of the list, but for some reason, whenever 12 and 2 are inputed like so Please enter your cards(x, y, z,...): 12, 2 it outputs

12, 2
12, 2

Here is the code:

#!/usr/bin/python2.7
cards_input = raw_input("Please enter your cards(x, y, z, ...): ")
print cards_input
cards = cards_input.split(", ")

minimum = min(cards)
cards.remove(min(cards))
cards.insert(0, minimum)

print cards

How can I fix this?

Carter Brainerd
  • 199
  • 2
  • 10

1 Answers1

6

You are currently comparing the numbers as STRINGS, eg: '12' < '2'.

Because '12' is < '2' when doing string comparison ('1' has a lower numerical value than '2') this means the evaluation will look funky.

What you want to do is compare integer values of these numbers, eg:

int('12') < int('2')

Change your code to the following:

#!/usr/bin/python2.7
cards_input = raw_input("Please enter your cards(x, y, z, ...): ")
print cards_input
cards = [int(x) for x in cards_input.split(", ")]

minimum = min(cards)
cards.remove(min(cards))
cards.insert(0, minimum)

print cards
rdegges
  • 32,786
  • 20
  • 85
  • 109