Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tidying Up
#1
Hi,

I'm not sure if I have cleaned up my code enough could I make it more tidy?

# PRINT ROWS # 
def printing(c):
  print("1: " + " ".join(c[0]))
  print("2: " + " ".join(c[1]))
  print("3: " + " ".join(c[2]))

# CARDS # 
row1 = ["KD","8S","5D","2D","3C","8D","JH",]
row2 = ["2S","7C","4H","2C","KS","AH","7H",]
row3 = ["6D","9D","KC","6C","7D","4D","9C",]
c = [row1, row2, row3]

# TRICK #
def cut(decknames):
    deck1= (c[0], c[3], c[6], c[9], c[12], c[15], c[18])
    deck2= (c[1], c[4], c[7], c[10], c[13], c[16], c[19])
    deck3= (c[2], c[5], c[8], c[11], c[14], c[17], c[20])
    pile = [deck1, deck2, deck3]
    return pile
def center(choice,decknames):
    while choice not in range(1,4):
        choice = int(input("Try again, Type the number row you card is in!"))     
        break
    if choice == 1:
        pile = (c[1] + c[0] + c[2])
    if choice == 2:
        pile = (c[0] + c[1] + c[2])
    if choice == 3:
        pile = (c[0] + c[2] + c[1])    
    return pile

# LOOPING #
printing(c)
loop = 0 
while loop < 3: 
    loop = loop + 1
    choice=int(input("Type the number row you card is in!"))
    c = center (choice, c)
    c = cut(c)
    printing(c)
print ("Your Card is", c[1][3])
Reply
#2
Note that slices can have three parts: the start, the end, and the step (how far forward it goes each time). So cut can be redone as:

def cut(decknames):
    deck1 = c[0::3]
    deck2 = c[1::3]
    deck3 = c[2::3]
    return [deck1, deck2, deck3]
You could even do that as a list comprehension:

def cut(decknames):
    return [c[start::3] for start in range(3)]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020