简体   繁体   中英

Trying to build a simple trivia type game. New to Python.

I'm trying to create a game I used to play as a kid. The premise for the game is that there is a group of players and they take turns guessing baseball players. You don't just randomly guess players, but you guess a player using the previous guessed players first initial of their last name. For example if a player guessed Alex Rodriguez an acceptable follow up guess is Randy Johnson. If the player is incorrect they are out of the game. This is a simple game and something I want to use to learn python. I've been doing tutorials from Code Academy and Learn Python the Hard Way, but now I'm ready to start creating something. What I've gotten so far is something that sorta works, but I can't figure out a way to pull in a player database from a website and how to remove players and correctly create a round about session of player guessing. I've included my code and I'm hoping someone out there is kind enough to help guide me on my first project!

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 "))

The problem is in the second while. Your recursive function is being called infinite times, making your program break. It should be an if...

Try this code:

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 "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM