vendredi 3 décembre 2021

why do I get error listOfStrings not defined when it's defined and taken as a parameter?

for python 3: I am trying to code a bingo game where each player has a card of 25 strings (in a 5x5 pattern) which are chosen randomly from a list. the list has 53 different items to choose from. im working on adding the players, and when each player is added I want to create their card. when I try player1card = random.sample(list(listOfStrings), 25) it just says that my list isn't defined even though I thought it was. can someone explain to me why? thank you in advance

import urllib.request
import random


def createList():
    listOfStrings = []
    try:
       with urllib.request.urlopen('https://www.cs.queensu.ca/home/cords2/bingo.txt') as f:
           d = f.read().decode('utf-8')
           split= d.split("\n")
           listOfStrings = split
    except urllib.error.URLError as e:
        print(e.reason)


def players(listOfStrings):
    noPlayers = input("How many players would like to play Bingo? Choose 1-3: ")
    if noPlayers == "1":
        name = input("What is the name of player 1? ")
        player1card = random.sample(list(listOfStrings), 25) 


createList()
players(listOfStrings)
print(player1card)

returns NameError: name 'listOfStrings' is not defined




Aucun commentaire:

Enregistrer un commentaire