'CherryB被声明两次,首先作为4个整数的列表,然后作为整数,play没有被定义'

2024-10-02 06:38:05 发布

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

如何解决这些问题

•CherryB声明两次,首先声明为4个整数的列表,然后声明为整数。 •CherriesT也有同样的问题 •游戏没有定义

import random
#Identify players
players = ['1', '2', '3', '4']
#Identify cherry trees
CherriesT = [10, 10, 10, 10]
#Identify Buckets
CherryB = [0, 0, 0, 0]
#Action result from each spin
spinChoice = [1, 2, 3, 4, -2, -2, -10]
#Number of cherries in the tree
CherriesT = 10
#How to win
winnerindex = 0
turns = 0

while CherryB < 10:
    for player in players:
        randNum = random.randint(0,6)
        PlayIn = players.index(player)
        print PlayIn
        spinResult = spinChoice[randNum]
        print spinResult
        CherriesT [PlayIn] = CherriesT [PlayIn] - spinResult
        print CherriesT [PlayIn]
        CherryB[PlayIn] = CherryB[PlayIn] + spinResult

        #Check number of cherries is between 0 and 10
        if CherryB[PlayIn] < 0:
            CherryB[PlayIn] = 0
        if CherriesT[PlayIn] > 10: 
            CherriesT[PlayIn] = 10

        play = play + 1
        if CherryB[PlayIn] == 10:
            winnerindex = PlayIn
        break

for player in players:
    if players.index(player)<=winnerindex:
        print player + " played " + str(play) + " times. Number of Cherries in"+player+"'s bucket:"+ str(CherryB[players.index(player)])
    else:
        print player + " played " + str(play-1) + " times. Number of Cherries in"+player+"'s bucket:"+ str(CherryB[players.index(player)])


print players[winnerindex] + " won after " + str(play) + " plays."

提前谢谢


Tags: ofin声明playindexifplayerprint
1条回答
网友
1楼 · 发布于 2024-10-02 06:38:05

一开始我不明白,但我想我解决了你的问题 随机导入

total = 0
counter = 0

while true:
    #Action result from each spin
    spinChoice = [1, 2, 3, 4, -2, -2, -10]
    turns = 0

    #Number of cherries in the tree
    cherriesT = 10
    for i in range(10000):
        while cherriesT >0:
            #Spin 
            randNum = random.randint(0,6) 
            spinResult = spinChoice[randNum]
            print 'The result of the spin is' + " " + str(randNum)

            #Add or remove cherries from the tree
            cherriesT = cherriesT - spinResult

            #Check number of cherries is between 0 and 10
            if cherriesT > 10:
                cherriesT = 10
            elif cherriesT < 0:
                cherriesT = 0

            print 'The number of cherries on the tree is' + " " + str(cherriesT)
            turns +=1

    print str(i)+': The number of turns to win was' + " " + str(turns)

    counter+=1;
    total+=turns
    print 'The average number of turns to win is' + " " + str(total/counter)#total/i?

相关问题 更多 >

    热门问题