我之前发布了一个代码,询问了我的Python3代码。 @philshem得到了一个满意的答案,但现在我的代码仍然没有用。这是代码:
#Dice Game
from random import randint
score = 0
running = True
while running:
print ('Rolling dice... rolling dice...')
print ('Done!')
rollcode = randint(1, 6)
if rollcode == randint:
print ('You scored: 1')
score = score + 1
running = False
else:
running = False
if rollcode == randint:
print ('You scored: 2')
score = score + 2
running = False
else:
running = False
if rollcode == randint:
print ('You scored: 3')
score = score + 3
running = False
else:
running = False
if rollcode == randint:
print ('You scored: 4')
score = score + 4
running = False
else:
running = False
if rollcode == randint:
print ('You scored: 5')
score = score + 5
running = False
running = False
if rollcode == randint:
print ('You scored: 6')
score = score + 6
running = False
else:
running = False
现在,当我运行它时,它不会打印出我想要的内容。它说
滚动骰子......滚动骰子...... 完成!但它永远不会打印你得分。
答案 0 :(得分:1)
在顶部附近,您有以下行:
rollcode = randint(1, 6)
然后,你所有的分支都在检查完全相同的东西:
if rollcode == randint:
randint
是一个函数,因为您为rollcode
分配了一个随机整数值,rollcode
永远不会正好是randint
看起来你想要做的是:
if rollcode == 1:
...
if rollcode == 2:
etc.
答案 1 :(得分:0)
这可能会做你想要的更少的代码并打印你的总分。
from random import randint
score = 0
running = True
while running:
#Generate Random number 1-6
print ('Rolling dice... rolling dice...')
rollValue = randint(1,6)
#Print roll value
print ('Done! \nYou scored: ' + str(rollValue))
#Add roll value to total score and print score
score += rollValue
print('Your total score is: ' + str(score))
#roll again (y or Y) or end program?
print ('Roll Again? [Y/N]')
answer = input().lower()
if answer != 'y':
running = False