【问题标题】:Returning Value from Function to Variable从函数返回值到变量
【发布时间】:2020-02-11 08:57:32
【问题描述】:

因此,我目前在弄清楚如何在我的 Rock, Paper, Scissors 游戏中基本创建“计数”功能时遇到了一些麻烦。我是 Python 新手,这是我第一次尝试使用多个函数来执行游戏逻辑。这是我的代码...

import random
from random import choice

cpu_score = 0
player_score = 0

# dictionary from which gestures are pulled
gestures = ["rock","paper","scissors"]

n_rounds = int(input("How many rounds would you like to play? "))

while n_rounds % 2 == 0 or n_rounds <= -1:
    print("Number of rounds must be an odd number! Select an odd number!")
    n_rounds = input("How many rounds?")
    break
print("Lets play",n_rounds,"rounds!")

rounds_to_win = ((n_rounds + 1)//2)
rounds_to_win = round(rounds_to_win)
print("You must win",rounds_to_win,"rounds to beat the game!")

def computer_choice():
    """Movement made by the computer"""
    comp = random.choice(gestures)   
    return comp

def player_gesture():
    """Movement by player"""
    player = input("Please select, rock, paper or scissors")
    if player not in gestures:
        print("That's not an option! Please try again.")
        player = input("Please select, rock, paper or scissors")  
    return player

def who_won_round(comp, player):
    '''Who is the winner of the round.'''

    winner = 0     

    if ((player == "rock") and (comp == "paper")) or \
            ((player == "paper") and (comp ==  "scissors")) or \
            ((player == "scissors") and (comp == "rock")):
          winner = 1

    elif ((comp == "rock") and (player == "paper")) or \
            ((comp == "paper") and (player == "scissors")) or \
            ((comp == "scissors") and (player == "rock")):
          winner = 2

    else:
        winner = 0

    return winner

def win_counter(winner, cpu_score,player_score):

    rounds = 1

    if winner == 1:
        player_score += 1
        print("This is round",rounds)
        rounds += 1
        return player_score

    if winner == 2:
        cpu_score += 1
        print("This is round",rounds)
        rounds += 1
        return cpu_score

def count_round(winner, player, comp, cpu_score, player_score):

    if winner == 0:
        print("It's a tie!")      

    elif winner == 1:

        print("The player chose",player)
        print("The computer chose",comp)
        print("The computer won the round!")
        print("The computer has won",cpu_score,"rounds!")

    elif winner == 2:
        print("The player chose",player)
        print("The computer chose",comp)
        print("The player won the round!")
        print("The player has won",player_score,"rounds!")

def game_structure():
    while rounds_to_win < n_rounds:
        comp = computer_choice()
        player = player_gesture()
        winner = who_won_round(comp,player)
        win_count = win_counter(winner, cpu_score,player_score)
        count = count_round(winner, player, comp, cpu_score, player_score)

game_structure()

基本上,我在返回变量时遇到问题,以便计算“回合数”、“cpu_score”和“player_score”的分数。我更喜欢不声明全局变量,因为我意识到它们使用起来可能很混乱,但我不太确定如何避免这种情况。

【问题讨论】:

    标签: python function variables


    【解决方案1】:

    如果必须避免使用全局变量,则应采用面向对象的方法。这样您就可以将变量存储在对象中。

    所以基本上你做这样的事情:

    newgame = mytictactoe()
    
    while True #infinite loop
        input("wanna play?")
        if input == yes:
            newgame.start
        else:
            break
    

    【讨论】:

      【解决方案2】:

      据我所知,cpu_scoreplayer_score 永远不会更新。您只有在win_count 中的最新结果,它不会随时分配给cpu_scoreplayer_score。可能是这样的:

      win_count = win_counter(winner, cpu_score,player_score)
      if winner == 1:
          cpu_score = win_count
      if winner == 2:
          player_score = win_count
      rounds_to_win = max(player_score, cpu_score)
      count_round(winner, player, comp, cpu_score, player_score)
      

      我没有运行它,但是这个修改应该可以解决问题。有更好的方法来做到这一点;也许使用列表来保持分数并使用获胜者作为索引,或者像其他人所说的那样使用对象方法,但我不想更改太多代码。

      另外,请记住win_counter 中的round 变量什么都不做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-02
        • 2020-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多