我很好奇是否有可能在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'
文件:
https://drive.google.com/file/d/0BxNi5bq6Cvnea0c4aVVIWUxZRUE/view?usp=sharing
我理解你的问题,一个简单明了的解决方案可能是更改
round
函数,以便在调用时,它的一个参数是对“回调函数”的引用。此函数将在调用round
的模块中定义,但实际上将在round
内部执行。你知道吗有关回调函数的详细信息: https://en.wikipedia.org/wiki/Callback_(computer_programming)#PythonImplementing a callback in Python - passing a callable reference to the current function 或者进一步搜索“python回调函数”
相关问题 更多 >
编程相关推荐