如何更改不同文件上的函数?

2024-09-26 18:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我很好奇是否有可能在Python中更改不同文件中的函数。你知道吗

什么psrock.py文件文件所做的是,它从每个文件中接收两个数据,这两个数据将在石头布剪刀中相互对抗,并决定哪个玩家赢了。当然,psrock.py文件文件也包含其他函数,但我只是插入了一个函数,因为其他函数对我的问题并不重要。你知道吗

我正在编辑中的函数psrock.py文件文件,以便第3组的(有第1组,第2组,第3组和他们玩石头布剪刀对彼此)。(即第一组和第二组互相对抗,然后第一组和第三组互相对抗。反之亦然,结果将永远是岩石和对手的结果将剪刀,以便3队可以赢得无论什么。你知道吗

但是,我在挣扎,不知道该怎么办。。:(我刚刚开始学习Python,这对我来说是一项非常有挑战性的任务。如果你能帮我一点忙,我会很高兴的;)

# This is a function from psrock.py file
import team3

def round(player1, player2, history1='', history2=''):

    # Get player 1's move. 
    move1 = player1(my_history=history1, their_history=history2)
    # Get player 2's move. 
    move2 = player2(my_history=history2, their_history=history1)

    if valid(move1) and valid(move2):      
        if move1 == move2:
            score1, score2 = 0, 0
        elif move1+move2 in ['rs', 'sp', 'pr']:
            score1, score2 = 1, -1
        else:
            score1, score2 = -1, 1
    else: #one of the moves was invalid
        if valid(move1): # only move2 was invalid
            move2 = 'x'
            score1, score2 = 1, -1
        elif valid(move2): # only move1 was invalid
            move1 = 'x'
            score1, score2 = -1, 1
        else: # Both moves invalid
            move1, move2 = 'x', 'x'
            score1, score2 = -1, -1

    return move1, move2, score1, score2

…我正试图从另一个名为team3的文件中编辑此函数。。。你知道吗

# Team 3 File
# -*- coding: utf-8 -*-
import psrock

def round(player1, player2, history1='', history2=''):
    move1 = player1(my_history=history1, their_history=history2)
    if player1 == team3:
        move1 = 'r'
        move2 = 's'
    elif player2 == team3:
        move1 = 's'
        move2 = 'r'

文件:


  1. 下载文件
  2. 解压缩zip文件
  3. 打开同一选项卡中的所有文件
  4. 玩psrock_播放.py文件

https://drive.google.com/file/d/0BxNi5bq6Cvnea0c4aVVIWUxZRUE/view?usp=sharing


Tags: 文件函数pyifhistoryplayer2player1score1

热门问题