试图建立一个简单的琐事类型的游戏。 Python新手。

时间:2015-05-09 06:03:49

标签: python

我试图制作一个我小时候玩过的游戏。游戏的前提是有一群玩家,他们轮流猜测棒球运动员。你不是随意猜测玩家,但你猜测一名玩家使用之前猜测过的玩家的姓氏首先出现。例如,如果一名球员猜到亚历克斯罗德里格兹,可接受的后续猜测是兰迪约翰逊。如果玩家不正确则他们不在游戏中。这是一个简单的游戏,我想用它来学习python。我一直在做Code Academy的教程和艰苦学习Python,但现在我已经准备好开始创作。到目前为止我得到的东西是有用的,但是我无法找到一种从网站上获取玩家数据库的方法,以及如何移除玩家并正确地创建一个关于玩家猜测会话的回合。我已经包含了我的代码,我希望有人能帮助指导我完成我的第一个项目!

def players(name):
    name_total = float(name)
    print name_total
    player = []
    while name_total > 0:
        player_name = raw_input("Enter Player Name ")
        player.append(player_name)
        name_total -= 1
    print player

player_database = ['Barry Bonds', 'Alex Rodriguez', 'Brad Ausmus']

def guess(player_guess):
    player_guess = player_guess
    if player_guess in player_database:
        print "Good guess!!"
        player_database.remove(player_guess)
        while player_database > 1:
            guess(raw_input("Guess a player"))
    else:
        print "You lose"
    return player_database


players(raw_input("How many players? "))
guess(raw_input("Guess a player "))

1 个答案:

答案 0 :(得分:0)

问题出在第二个问题。 您的递归函数被无限次调用,使您的程序中断。 它应该是一个if ...

试试这段代码:

def players(name):
    name_total = float(name)
    print name_total
    player = []
    while name_total > 0:
        player_name = raw_input("Enter Player Name ")
        player.append(player_name)
        name_total -= 1
    print player

player_database = ['Barry Bonds', 'Alex Rodriguez', 'Brad Ausmus']

def guess(player_guess):
    player_guess = player_guess
    if player_guess in player_database:
        print "Good guess!!"
        player_database.remove(player_guess)
        if player_database > 1:
            guess(raw_input("Guess a player"))
    else:
        print "You lose"
    return player_database


players(raw_input("How many players? "))
guess(raw_input("Guess a player "))