2017-06-07 91 views
-7

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

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

谢谢!!

+2

没有什么错误之间保持兼容它提高?为什么在最后一行的末尾有'}'?为什么你的if语句中有'x'资本? – rassar

+0

引发的错误是第1行NameError:未定义玩家。 X在练习中是大写的(这是其中的一种学习练习,那''''最后是我的错误 – StudentFL01

+0

你还没有定义什么是球员???实际上球员是什么?它是一个随机数列表 – zaidfazil

回答

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

+0

谢谢anatol;给我很多提及反对。非常感激 – StudentFL01