简体   繁体   中英

How do I repeat a function but get it to return different values on each repetition? (Python)

I am new here and this is quite hard for me to explain so please bear with me, I will try be as specific as possible!

I am simulating a game of top trumps, and I have defined a function 'playgame()' which plays 1 game. I need to play loads of games (say, 10000), but every way I have tried to do this (ie in a for loop, in a while loop, using the repeat function etc) it just plays the exact same game 10000 times. The outcome of the game depends on the cards in the deck (which are made at random in a different function), but on each repetition it uses the and deals the cards in the , so the outcome is the same each time. 的卡组并以卡,因此结果相同每一次。

What I am wanting to happen, is for the script to 'play' 10000 games, so I can collect data from these games, rather than 10000 repetitions of the same game (as this is useless information). 游戏,所以我可以从这些游戏中收集数据,而不是同一游戏的10000次重复(因为这是无用的信息)。

Is there any way of repeating a function which you have defined yourself, but getting it to use different values each time?

Here is my function:

def playgame():
    i=0
    a=0
    b=0
    while len(Player_0_Deck) and len(Player_1_Deck) >= 1:
        if len(Player_0_Deck) and len(Player_1_Deck) >= 1:
            Round_P0()
            a = a + 1
        if len(Player_0_Deck) and len(Player_1_Deck) >= 1:
            Round_P1()
            b = b + 1
        i = i + 1
        if i >= 100:
            break
    print a + b, "rounds have been played in this game."

Round_P0() and Round_P1() are defined here:

def Round_P0():
a=1
Chosen0 = max(Player_0_Deck[0])
if Chosen0 is Player_0_Deck[0][0]:
    Matched1 = Player_1_Deck[0][0]
else:
    Matched1 = Player_1_Deck[0][1]
print "Player 0's chosen value:", Chosen0
print "Player 1's corresponding value:", Matched1
print "         "
if Matched1 <= Chosen0:
    Player_0_Deck.insert(len(Player_0_Deck)-1, Player_0_Deck.pop(0))
    Player_0_Deck.append(Player_1_Deck[0])
    Player_1_Deck.remove(Player_1_Deck[0])
    a = a + 1
if Matched1 > Chosen0:
    Player_1_Deck.insert(len(Player_1_Deck)-1, Player_1_Deck.pop(0))
    Player_1_Deck.append(Player_0_Deck[0])
    Player_0_Deck.remove(Player_0_Deck[0])
    a = a + 1
print Player_0_Deck
print Player_1_Deck

def Round_P1():
b=0
Chosen1 = max(Player_1_Deck[0])
if Chosen1 is Player_1_Deck[0][0]:
    Matched0 = Player_0_Deck[0][0]
else:
    Matched0 = Player_0_Deck[0][1]
print "Player 1's chosen value:", Chosen1
print "Player 0's corresponding value:", Matched0
print "             "
if Matched0 <= Chosen1:
    Player_1_Deck.insert(len(Player_1_Deck)-1, Player_1_Deck.pop(0))
    Player_1_Deck.append(Player_0_Deck[0])
    Player_0_Deck.remove(Player_0_Deck[0])
    b = b + 1
if Matched0 > Chosen1:
    Player_0_Deck.insert(len(Player_0_Deck)-1, Player_0_Deck.pop(0))
    Player_0_Deck.append(Player_1_Deck[0])
    Player_1_Deck.remove(Player_1_Deck[0])
    b = b + 1
print Player_0_Deck
print Player_1_Deck

And in case it is needed, this is how I make my deck/deal hands:

N = int(raw_input("Choose the number of total cards you would like to play \
with. (It must be even.) "))

def MakeCard():
    A = random.randint(1,N)
    Yin = A
    Yang = N+1-A
    return (Yin, Yang)

def MakeDeck():
    return [MakeCard() for i in range(1,N+1)]

Deck = MakeDeck()

#Deal cards

random.shuffle(Deck)
print "This is the complete deck:", Deck
Player_0_Deck = Deck[0:N/2]
Player_1_Deck = Deck[N/2:N+1]
print "This is Player 0's hand:", Player_0_Deck
print "This is Player 1's hand:", Player_1_Deck

I hope this makes sense, any help would be appreciated!

The deck object gets passed to your function at definition time. It isn't linked back and forth. Pass the deck n as an argument and create a new deck for each iteration of the loop like

for _ in range(10000):
    theDeck = MakeDeck()
    playgame(theDeck)

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