Adding element to a dictionary in python?

天涯浪子 提交于 2020-02-23 09:46:31

问题


I'm relatively new here, so please tell me if there is anything I should know or any mistakes I am making manner wise!

I am trying to add things onto a dictionary through random choice, but my code doesn't seem to work!

The file: sports.txt

Soccer, Joshua
Lacrosse, Naome Lee
Soccer, Kat Valentine
Basketball, Huong
Tennis, Sunny
Basketball, Freddie Lacer

my code so far:

def sportFileOpen():

    sportFile = open("sport.txt")
    readfile = sportFile.readlines()
    sportFile.close()
    return(readfile)


def sportCreateDict(sportFile):

    sportDict = {}

    for lines in sportFile:
        (sport, name) = lines.split(",")

        if sport in sportDict:
            sportDict[sport].append(name.strip())

        else:
            sportDict[sport] = [name.strip()]


    return(sportDict)



def sportRandomPick(name, sport, sportDict):


    if sport in sportDict:

        ransport = random.choice(sportDict.keys())

        sportDict[ransport].append(name)


        print(name, "has been sorted into", ransport)


def main():

    sportFile = sportFileOpen()

    sportDict = sportCreateDict(sportFile)


    name = input("Enter the name: ")

    preferredSport = input("Which sport do they want? ")

    sportRandomPick(name, preferredSport, sportDict)


main()

I am trying to allow a user to input their name and preferred group of sport, and whatever sport they prefer will have a higher chance of being randomly picked then the others (for example if Jason chooses soccer his chances of getting in soccer may double).

I don't expect anyone to write code for me, I know it's time consuming and you have better things to do! But can anyone maybe explain to me how I would go about doing this? I understand how to make random choices but I don't know how I would "double" the chances.

Also I keep getting this error when running my code: NameError: global name 'random' is not defined

I thought I was doing that part right but now i'm stuck. Can anyone give their two cents on this?


回答1:


Try this:

def sportRandomPick(name, sport, sportDict):
    if sport in sportDict:
        ransport = random.choice(list(sportDict.keys()) + [sport]) # list of sports will contain preferred sport twice.

        sportDict[ransport].append(name)

        print(name, "has been sorted into", ransport)

This will increase chances of preferred sport to be picked by 2.

And don't forget to import random




回答2:


I am assuming you are trying to use random.choice from python random.choice

you need to make sure it is imported at the top of your file:


import random

def sportRandomPick(name, sport, sportDict):


    if sport in sportDict:

        ransport = random.choice(sportDict.keys())

        sportDict[ransport].append(name)


        print(name, "has been sorted into", ransport)


来源:https://stackoverflow.com/questions/40776193/adding-element-to-a-dictionary-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!