tidying Up - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: tidying Up (/thread-4817.html) |
tidying Up - SummoningZ - Sep-10-2017 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]) RE: tidying Up - ichabod801 - Sep-10-2017 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)] |