如何在python中使用函数和循环调用变量

如何在python中使用函数和循环调用变量,python,python-3.x,list,Python,Python 3.x,List,我对Python很陌生,不熟悉语法。因此,目前,我正在尝试创建一个非常简单的刽子手游戏,这样游戏将根据所选类别从单词列表中随机选择单词。但是,我不知道如何调用我使用函数和循环创建的变量(单词列表1、2和3) import random, sys from typing import List # TODO try to load these from a text file WORD_LIST1 = ["tables","ladders","chairs"] WORD_LIST2= ["ch

我对Python很陌生,不熟悉语法。因此,目前,我正在尝试创建一个非常简单的刽子手游戏,这样游戏将根据所选类别从单词列表中随机选择单词。但是,我不知道如何调用我使用函数和循环创建的变量(单词列表1、2和3)

import random, sys
from typing import List

# TODO try to load these from a text file
WORD_LIST1 = ["tables","ladders","chairs"]
WORD_LIST2=  ["chicken","dog","cat"]
WORD_LIST3=  ["basketball","soccer","rugby"]


category=["objects","animals","sports"]
def category():
    categoryname = input("Please select a category\n").strip()
    for x in range(3):
        if categoryname=category(x)

            #How to create function to call the word_list 1 2 and 3 here?

        else:
            print("wrong category")
            tryagain=input("Choose another category\n").strip()

您可以简单地将其作为常规参数传递。不需要额外的东西。您可以阅读有关解包变量参数的内容,但我认为这有点高级,您不需要它

在这里你可以做

processUserGuesses(WORD_LIST1)

在这里,您可以使用python中的字典。请参阅下面的代码

category_lists = {
    "objects": ["tables", "ladders", "chairs"],
    "animals": ["chicken", "dog", "cat"],
    "sports": ["basketball", "soccer", "rugby"]

}

def category():
    categoryname = input("Please select a category\n").strip()
    if categoryname in category_lists:
        word_list = category_lists.get(categoryname)
        # do what ever you want with the list
        SECRET_WORD = random.choice(word_list)
        print(SECRET_WORD)
    else:
        print("wrong category")

如果需要,可以从JSON文件或某些外部源加载类别

你可以试试这个。如果用户输入了错误的类别,此版本的程序将不断提示用户仅输入允许的类别值。最后,当用户输入允许的类别时,它将打印该类别的随机条目

import random

# Store the category and values into a dictionary
categories = {
    "objects": ["tables", "ladders", "chairs"],
    "animals": ["chicken", "dog", "cat"],
    "sports": ["basketball", "soccer", "rugby"]

}

def category():
    print("Please enter category name: ")
    response = ''

    #Keep prompting the user to only enter allowed category values
    while response.lower() not in categories:
        # join(map(str, list((*categories,))) is used for retrieving the key values i.e. the category values from the dictionary "categories" and then join them as a string in order to display the allowed values back to the user
        response = input(' One among the following [%s] : \n' % ', '.join(map(str, list((*categories,)))))

    if response in categories:
        word_list = categories.get(response)
        # Print a random value from the chosen category
        print(random.choice(word_list))

# Call the function
category()

你所谓的变量是什么意思?如果您只想打印单词列表,可以在函数中使用全局单词列表1来访问它。另外,您不需要for循环来检查用户输入,只需执行:if categoryname in category:请澄清您要执行的操作。您的代码格式不正确–缩进错误,某些部分不是有效的Python(例如,如果categoryname=category(x))。调用变量是什么意思?可以调用函数,也可以将变量作为参数传递到函数中。这就是你想要做的吗?@SubhashR因为到目前为止我学到的唯一一件事就是从函数中调用变量,所以我不确定还有什么其他选项可用。@MisterMiyagi oh ya,我忘了python是缩进敏感的,我的错…对你的第二个问题…你可以学习更多关于python字典的知识来解决这些挑战。在下面的答案中添加了一个示例。当我键入一个类别列表(如“对象”)时,什么也没有发生。因此,我可以在这里假设这是一个有效的响应吗?(好像它意味着程序从这个类别中为《刽子手》游戏选择了一个随机词?)我尝试使用您创建的类别列表,并使用SECRET\u word=random.choice(类别列表)将其随机化我得到了关键错误:1,我不确定我做错了什么。get(categoryname)获取特定categoryname的单词列表。你必须从这个列表中随机选择。我用一个示例代码更新了答案。对不起,如果你不介意的话,我可以问一下是什么。join(map(str,list((*categories,)))?join(map(str,list(*categories,))用于从字典“categories”中检索键值,即类别值,然后将它们作为字符串连接起来,以便将允许的值显示回用户