Python 3.4骰子滚动模拟器代码错误
问题描述:
所以我有我的代码在这里为我使用PyDev在Eclipse霓虹灯Python 3.4中创建的骰子滚动模拟器游戏。当你猜一个数字,从1-6的值将随机生成,如果你得到多少吧,你继续前进等Python 3.4骰子滚动模拟器代码错误
guess1 = input("Enter your first guess as to which side the dice will roll on (1-6): ")
import random
dice_side = ['1','2','3','4','5','6']
game = print(random.choice(dice_side))
if guess1 == game:
print("Congrats that's the correct guess!")
else:
print("That's the wrong guess!")
我测试了代码,每次我提出一个数字,控制台始终打印“这是错误的猜测!”。即使我猜的数字与生成的数字相匹配。我无法弄清楚这有什么问题。我在想,也许我应该使用而不是循环。但是,我想知道我是否可以这样做,这个特定的代码有什么问题。我是Python的新手,所以任何帮助表示赞赏。提前致谢!
答
print()
返回None
。如果您在该行之后打印game
,则会看到它的价值。要解决:
game = random.choice(dice_side)
答
为了理解为什么你的游戏不工作,你应该试着先了解什么是错的这条线game = print(random.choice(dice_side))
。这是你的游戏的修改版本,这将给你一些线索,试图运行它,了解它,那么就检查你的脚本:
import random
dice_side = ['1', '2', '3', '4', '5', '6']
game = random.choice(dice_side)
print("Dice has been rolled... :D {0}".format(game))
guess = -1
number_attempts = 0
while True:
guess = raw_input("Which side the dice has rolled on (1-6): ")
number_attempts += 1
if guess == game:
print("Congrats that's the correct guess! You've tried {0} times".format(
number_attempts))
break
print("That's the wrong guess!")
一个建议,一旦你发现了,为什么不工作,只是尝试添加越来越多的新功能,直到你的游戏变得真正上瘾和有趣......但最重要的是,只是玩得开心:)
想想'print'返回什么。 – user2357112