Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将计数器值更改为字符串_Python_Python 3.x - Fatal编程技术网

Python 将计数器值更改为字符串

Python 将计数器值更改为字符串,python,python-3.x,Python,Python 3.x,这个项目是在一个小时内到期,我已经尝试了每一种方法,我知道,但我只是不能得到这一部分的工作。我真的需要帮助。我想在结果相加后更改计数器值,所以我想打印一个字符串,而不是打印每个计数器的总数 # Compare countA to distance: def raceAlgorithm(jack, slow, thunder) : # Race start point countA = 0 # Length of the race distance = 20

这个项目是在一个小时内到期,我已经尝试了每一种方法,我知道,但我只是不能得到这一部分的工作。我真的需要帮助。我想在结果相加后更改计数器值,所以我想打印一个字符串,而不是打印每个计数器的总数

# Compare countA to distance:
def raceAlgorithm(jack, slow, thunder) :
    # Race start point
    countA = 0

    # Length of the race
    distance = 20

    # message afer race is done
    msg = '\nRace Finished\nGetting results.'

    while countA != distance :
        dice = randint( 1, 6 )
        sleep( 1 )
        countA += 1

        if dice <= 2 :
            jack += 1
            # print(a)
        elif dice <= 4 :
            slow += 1
            # print(b)
        else :
            thunder += 1
            # print(c)
        if jack + slow + thunder == distance :
            print( msg )
            Position.append(jack)
            Position.append( slow )
            Position.append( thunder )
            Position.sort( reverse=True )

            # the horses totals
            print( *Position, sep='\n' )

            if Position[0] == jack:
                print( '\nThe winner is: ' + a )
            elif Position[0] == slow :
                print( '\nThe winner is: ' + b )
            elif Position[0] == thunder :
                print( '\nThe winner is: ' + c )
            else :
                print( )

#将countA与距离进行比较:
def算法(杰克、慢、雷):
#比赛起点
countA=0
#比赛长度
距离=20
#比赛结束后的消息
msg='\n查看已完成的\n设置结果。'
而countA!=距离:
骰子=randint(1,6)
睡眠(1)
countA+=1

如果dice将
位置
列表更改为同时包含名称和值的列表,则可以按值排序并同时打印

from operator import itemgetter

Position = [('jack', jack), ('slow', slow), ('thunder', thunder)]
Position.sort(key=itemgetter(1), reverse=True)
print(*Position)
print('\nThe winner is:', Position[0][0])

我指的是打印(*位置)列表。那里的一个很有效。如果你使用一个元组或字典列表,那就更好了,然后你可以把马的名字和分数放在一起。相信我,我知道这可以做得更好,但不幸的是,我们的讲师没有涉及太多复杂的事情,所以我们只能自己去做。我基本上是个新手,所以我来这里。我该如何去实现计数器的价值呢。我再也不能说你是个传奇了