识别python错误

时间:2017-06-07 12:20:25

标签: python compiler-errors

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

我是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."}

谢谢!

1 个答案:

答案 0 :(得分:0)

适用于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之间的兼容性。