Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looping through variables in Python

Edit: new here so advice on correct protocol welcome and apologies if I’m not following it but this was solved very quickly by the wonderful people of Substack!

I’ve written a program to randomly assign teams to four people for a World Cup sweepstakes. The code works fine, but it’s ugly. How do I shuffle the variables Seed1, Seed2, … Seed8 with a loop? How do I print which teams have been assigned to each player with more professional looking code?

import random

Teams = ["Alice", "Bob","Charlie", "Delilah"]

random.shuffle(Teams)

Seed1 = ["Brazil", "Argentina", "France", "Spain"]
Seed2 = ["England", "Germany", "Netherlands", "Portugal"]
Seed3 = ["Belgium", "Denmark", "Uruguay", "Croatia"]
Seed4 = ["Serbia", "Switzerland", "Senegal", "Mexico"]
Seed5 = ["USA", "Poland", "Ecuador", "Morocco"]
Seed6 = ["Wales", "Japan", "Ghana", "Canada"]
Seed7 = ["Qatar", "South Korea", "Iran", "Cameroon"]
Seed8 = ["Australia", "Saudi Arabia", "Tunisia", "Costa Rica"]

random.shuffle(Seed1)
random.shuffle(Seed2)
random.shuffle(Seed3)
random.shuffle(Seed4)
random.shuffle(Seed5)
random.shuffle(Seed6)
random.shuffle(Seed7)
random.shuffle(Seed8)

for a in range(4):
    print(Teams[a] + " = " +Seed1[a]+ " , " + Seed2[a]+ " , " + Seed3[a]+ " , " + Seed4[a]+ " , " + Seed5[a]+ " , " + Seed6[a]+ " , " + Seed7[a]+ " , " + Seed8[a])

To be clear I’m looking for something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for a in range(1,9):
   random.shuffle(range[a])

but that throws up an error

>Solution :

I would suggest putting the seeds is a list:

import random

teams = ["Alice", "Bob", "Charlie", "Delilah"]
random.shuffle(teams)

seeds = [
    ["Brazil", "Argentina", "France", "Spain"],
    ["England", "Germany", "Netherlands", "Portugal"],
    ["Belgium", "Denmark", "Uruguay", "Croatia"],
    ["Serbia", "Switzerland", "Senegal", "Mexico"],
    ["USA", "Poland", "Ecuador", "Morocco"],
    ["Wales", "Japan", "Ghana", "Canada"],
    ["Qatar", "South Korea", "Iran", "Cameroon"],
    ["Australia", "Saudi Arabia", "Tunisia", "Costa Rica"],
]
for seed in seeds:
    random.shuffle(seed)

for i in range(4):
    print(f"{teams[i]} =", ", ".join(seed[i] for seed in seeds))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading