有人可以协助您识别此循环中的错误吗?

我对Python完全陌生。

for p in players:
    x = len(p)
    dice = random.randint(0, x*2)
    print p + " rolls a " + dice + " out of " + str(x*2)
    if dice > x:
       print p + " wins!"
    elif dice == x:
           print p + "gets a tie."
    else:
           print p + " loses."}

谢谢!!

最佳答案

在Python 3.x中工作

import random

players = ["Player 1", "Player 2"]

for p in players:
    x = len(p)
    dice = random.randint(0, x*2)
    print(str(p) + " rolls a " + str(dice) + " out of " + str(x*2))
    if dice > x:
        print(p + " wins!")
    elif dice == x:
        print(p + "gets a tie.")
    else:
        print(p + " loses.")

对于Python 2.x
import random

players = ["Player 1", "Player 2"]

for p in players:
    x = len(p)
    dice = random.randint(0, x*2)
    print(str(p) + " rolls a " + str(dice) + " out of " + str(x*2))
    if dice > x:
        print p + " wins!"
    elif dice == x:
        print p + "gets a tie."
    else:
        print p + " loses."

或在第一个示例中添加from __future__ import print_function以确保Python 3和2之间的兼容性。

关于python - 识别python错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44412465/

10-12 00:29
查看更多