gpt4 book ai didi

python - 识别python错误

转载 作者:行者123 更新时间:2023-12-02 11:15:53 26 4
gpt4 key购买 nike

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

我对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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com