Paul Alexander Burkart
Paul Alexander Burkart

Reputation: 494

Confused by my Output

Currently I'm just creating a casino in Python. Just to fool around, and I've come to not so much a problem. But confusion. I've been trying to figure this out for a while now. My output doesn't follow any noticeable pattern but its the same every time.

import random
import time
import string
import os
import platform

def clear():
    operSys = platform.system()
    if operSys.lower() == "Windows":
        os.system('cls')
    else:
        os.system('clear')

cards = {"Ace of Spades":'1',
         "Ace of Clubs":'1', 
         "Ace of Diamonds":'1', 
         "Ace of Hearts":'1', 

         "Two of Spades":'2', 
         "Two of Clubs":'2', 
         "Two of Diamonds":'2',
         "Two of Hearts":'2',

         "Three of Spades":'3',
         "Three of Clubs":'3',
         "Three of Diamonds":'3',
         "Three of Hearts":'3',

         "Four of Spades":'4',
         "Four of Clubs":'4',
         "Four of Diamonds":'4',
         "Four of Hearts":'4',

         "Five of Spades":'5',
         "Five of Clubs":'5',
         "Five of Diamonds":'5',
         "Five of Hearts":'5',

         "Six of Spades":'6',
         "Six of Clubs":'6',
         "Six of Diamonds":'6',
         "Six of Hearts":'6',

         "Seven of Spades":'7',
         "Seven of Clubs":'7',
         "Seven of Diamonds":'7',
         "Seven of Hearts":'7',

         "Eight of Spades":'8',
         "Eight of Clubs":'8',
         "Eight of Diamonds":'8',
         "Eight of Hearts":'8',

         "Nine of Spades":'9',
         "Nine of Clubs":'9',
         "Nine of Diamonds":'9',
         "Nine of Hearts":'9',

         "Ten of Spades":'10',
         "Ten of Clubs":'10',
         "Ten of Diamonds":'10',
         "Ten of Hearts":'10',

         "Jack of Spades":'11',
         "Jack of Clubs":'11',
         "Jack of Diamonds":'11',
         "Jack of Hearts":'11',

         "Queen of Spades":'12',
         "Queen of Clubs":'12',
         "Queen of Diamonds":'12',
         "Queen of Diamonds":'12',

         "King of Spades":'13',
         "King of Clubs":'13',
         "King of Diamonds":'13',
         "King of Hearts":'13'}

for x in cards:
    print x + ' : ' + cards[x]

Output:

Queen of Clubs : 12
Ten of Hearts : 10
Three of Spades : 3
Nine of Spades : 9
Ace of Clubs : 1
King of Spades : 13
Ace of Spades : 1
Queen of Diamonds : 12
Three of Hearts : 3
Seven of Hearts : 7
Two of Spades : 2
Two of Hearts : 2
Five of Hearts : 5
Three of Clubs : 3
Queen of Spades : 12
Two of Clubs : 2
Four of Diamonds : 4
King of Diamonds : 13
Ace of Diamonds : 1
Five of Diamonds : 5
Four of Spades : 4
Six of Spades : 6
Ten of Spades : 10
Eight of Clubs : 8
Jack of Clubs : 11
Six of Diamonds : 6
Two of Diamonds : 2
Eight of Hearts : 8
Jack of Hearts : 11
Nine of Hearts : 9
King of Hearts : 13
Ace of Hearts : 1
Seven of Spades : 7
Six of Hearts : 6
Ten of Clubs : 10
Five of Spades : 5
Jack of Spades : 11
Eight of Spades : 8
Ten of Diamonds : 10
Four of Clubs : 4
Eight of Diamonds : 8
Jack of Diamonds : 11
Six of Clubs : 6
King of Clubs : 13
Three of Diamonds : 3
Nine of Clubs : 9
Nine of Diamonds : 9
Seven of Diamonds : 7
Four of Hearts : 4
Five of Clubs : 5
Seven of Clubs : 7

Upvotes: 1

Views: 90

Answers (3)

jez
jez

Reputation: 15339

dict order is arbitrary. Try this instead:

for k, v in sorted(cards.items()):
  print(k+':'+v)

That sorts by key. Sorting by item value takes a step or two more:

reversed = [(v,k) for k, v in cards.items()]
for v,k in sorted(reversed):
       print(k+':'+v)

Also btw: the .lower() output of any string will never match a mixed-case string like 'Windows'.

Upvotes: 2

mchfrnc
mchfrnc

Reputation: 5713

You mean dictionary cards is your pattern? If so, this is wrong, because dictionary in Python is an unordered structure (output could be different than your "pattern").

Upvotes: 0

Holger Just
Holger Just

Reputation: 55718

Dictionaries in Python (and most other languages) don't have an implicit order. They are actually intended for unordered key-value pairs and don't even attempt to preserve the order of key-value pairs. As a data-structure, the goal is to look up values as cheaply as possible for which other traits (like the order) is sacrificed.

As such, if you actually want an order, you could put the keys into an ordered array and look the values up from the dictionary or even use an array to store all your data in the first place.

Upvotes: 4

Related Questions