0

I have been working on some code within python, and im trying to have a end="" print out a whole list on the same line. the list will then receive more values and delete the old ones. i want these new values to be printed on a new line, and i want to print the whole list on one line. then once again i would like the values to be deleted and new ones to be added, in a loop until all the values have been used. heres the code i wrote

 def print_deck():
        Deck=generate_deck()
        Print_Deck=[]
        j=0
        if j<53:
            for k in range(13):
                for i in range(4):
                    print(Deck[j], end="   ")
                    j=j+1

when i run my script i get everything on one line, though im trying to only have 4 values from the list per line. here is the output

king of Spades King of Clubs King of Diamonds King of Hearts Queen of Spades Queen of Clubs Queen of Diamonds Queen of Hearts Jack of Spades Jack of Clubs Jack of Diamonds Jack of Hearts Ace of Spades Ace of Clubs Ace of Diamonds Ace of Hearts 2 of Spades 2 of Clubs 2 of Diamonds 2 of Hearts 3 of Spades 3 of Clubs 3 of Diamonds 3 of Hearts 4 of Spades 4 of Clubs 4 of Diamonds 4 of Hearts 5 of Spades 5 of Clubs 5 of Diamonds 5 of Hearts 6 of Spades 6 of Clubs 6 of Diamonds 6 of Hearts 7 of Spades 7 of Clubs 7 of Diamonds 7 of Hearts 8 of Spades 8 of Clubs 8 of Diamonds 8 of Hearts 9 of Spades 9 of Clubs 9 of Diamonds 9 of Hearts 10 of Spades 10 of Clubs 10 of Diamonds 10 of Hearts

I would like it like this:

king of Spades King of Clubs King of Diamonds King of Hearts Queen of Spades Queen of Clubs Queen of Diamonds Queen of Hearts
Jack of Spades Jack of Clubs Jack of Diamonds Jack of Hearts
Ace of Spades Ace of Clubs Ace of Diamonds Ace of Hearts ect ect

Any help would be amazing, ive been sitting here for the past 4 hours trying to figure this out.

2
  • I suspect you actually want if j in range(4,5,4) instead of j == range(4,52,4) Commented Nov 19, 2017 at 3:01
  • Just put a print() line where you want the line break Commented Nov 19, 2017 at 3:02

1 Answer 1

1

I think what you wanted was something like this:

def print_deck():
  Deck=generate_deck()
  Print_Deck=[]    
  j=0
  for k in range(13):
    for i in range(4):
      print(Deck[j], end="   ")
      j=j+1
    print(" ")

also the if j < 53: is unneccessary as the previous line sets j value to zero

Sign up to request clarification or add additional context in comments.

3 Comments

thanks so much for this, instantly fixed my function. just wondering does putting print(" ") after the 2nd for loop in the 1st for loop stop the end=" " from adding more strings to the current
@JarrahBaldwinCaesar The end statement allows you to use a custom ending character, in this case you were using three spaces. The default ending character for the print() function is \n. You can see this in the python docs docs.python.org/3/library/functions.html#print
@JarrahBaldwinCaesar If my answer solved your problem, please click the big checkbox to accept it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.