首页 > 解决方案 > 为什么从 Python 中的字典访问时函数不执行

问题描述

我正在尝试制作一个 python 程序来执行用户从一堆选项中选择的游戏。为此,我创建了一个存储游戏名称及其各自功能的字典,如下所示:

def Game():
    def hangman():
        import Hangman
    games={"a":{"Hangman":hangman()},"b":{"Tic Tac Toe(2 Players)":"TTT"},"c":{"Stone Paper Scissors":"SPS"},"d":{"Guess The Number":"GTN"}}
    print("Which Game would you like to play?(a,b,c,d,e):")
    for choice in games:
        for Game_Name in games[choice]:
            print(choice+":"+Game_Name)
    answer=input()
    if answer.lower() in games:
        for Game_Name in games[answer.lower()]:
            print("You chose "+Game_Name)
            __name__="__main__"
            return games[answer.lower()][Game_Name]

如果给定输出"a",它应该返回函数 hangman 但是当我运行程序时,我输入输出 "a" 但是该函数没有被执行。输出是:

 NOT
Which Game would you like to play?(a,b,c,d,e):
a:Hangman
b:Tic Tac Toe(2 Players)
c:Stone Paper Scissors
d:Guess The Number
a
You chose Hangman

Hangman文件代码如下:

import random
if __name__ == '__main__':
    Fruits =["apple","banana","mango","strawberry","orange","grape","pineapple","apricot","lemon","coconut","watermelon","cherry","papaya","berry","peach","lychee","muskmelon"]
    Applauds=["Good Job!","Correct!","Keep Going"]
    Try=["Try Again","Wrong Answer","You Bad"]
    Wins=0
    Scores=[]
    def Word(Guess,Enter=False):
        global word_guess
        global wrong
        global word
        global word_length
        if Enter:
            if Guess in word:
                new=""
                print(random.choice(Applauds))
                for i in range(word_length):
                    if Guess==word[i]:
                        new+=word[i]
                    else:
                        new+=word_guess[i]
                print(new)
                word_guess=new
            else:
                print(random.choice(Try))
                print(word_guess)
                wrong+=1
        else:
            print(word_guess)
    def Main():
        global word_guess
        global word
        global word_length
        global Wins
        global wrong
        global Scores
        word=random.choice(Fruits)
        picked=[]
        picked.append(Fruits.pop(Fruits.index(word)))
        word_length=len(word)
        word_guess="-"*word_length
        max_wrong=word_length+2
        Word("Nothing")
        print("\nGuess the word!HINT:Fruit")
        Chance=1
        wrong=0
        used=[]
        while wrong<=max_wrong and word_guess!=word:
            print("The word has",word_length,"Letters")
            print("You have",str(max_wrong-wrong),"chances")
            a=input("Guess No."+str(Chance)+":")
            if a in used:
                print(a,"has already been picked!Guess again")
            else:
                used.append(a)
                Word(a,Enter=True)
            Chance+=1
        else:
            if word_guess==word:
                print("You have successfully guessed the word",word,"with",wrong,"wrong answers!")
                Scores.append(wrong)
                Wins+=1
            else:
                print("You lost!The word was",word)
            Y_or_N=input("Do you wish to continue to the next word?:")
            if Y_or_N in ["Yes","yes","Y"]:
                Main()
            else:
                print("You won",Wins,"Game!")
                print("Thanks for Playing")
    Main()
else:
    print("NOT")

我在函数定义后使用“if name =” main语句不直接执行。请帮助!

标签: pythonfunctiondictionaryimportoutput

解决方案


推荐阅读