我正在尝试学习更多python,并且正在尝试创建一个骰子游戏。我的问题是,当它在IDLE中运行时,它会在输入的初始yes之后循环。任何人都可以给我一些提示/帮助吗?
如果我改变
roll = input('Would you like to play a game?')
要
roll = "yes"
它完全启动,并立即结束脚本。
这是我的完整代码
import random
min = 0
max = 20
i = random.randint(min,max)
roll = input('Would you like to play a game?')
while roll == "yes":
print ('''
======================================
You run into a deadly demonic intity.
You must role to save your life.
You must role higher than a 10 to win.
======================================
''')
for i in range(1):
print (random.randint(min,max))
if i >= 10:
print ('''Your staff begins to hum as you say your incantation.
The demonic intitiy, begins to shreak with a blood curtling sound.
You stand your ground, and banish it!''')
elif i <= 10:
print ('''You watch in dispair, as the intity devours your friends.
You stand their, with no where to run, knowing that this is the end...''')
if roll == "no":
print ('Guess you could run too...')
我希望它抓取random.randint,并输出响应语句。我也注意到即使它循环,它也会完成跳过
if i >=10:
然后循环回复
elif i <=10:
即使random.randint是20。
答案 0 :(得分:1)
一些问题:
你正在调整I,它总是0或1:
对于范围(1)中的i:
这将始终为0或1。 相反,您应该检查实际骰子卷,例如:
dice_value=(random.randint(min,max))
如果您有任何其他问题,请使用此代码:
import random
min = 0
max = 20
i = random.randint(min,max)
roll = raw_input('Would you like to play a game? ')
print (roll)
while roll == 'yes':
print ('''
======================================
You run into a deadly demonic intity.
You must role to save your life.
You must role higher than a 10 to win.
======================================
''')
dice_value=(random.randint(min,max))
print ("You Rolled " + str(dice_value))
if dice_value >= 10:
print ('''Your staff begins to hum as you say your incantation.
The demonic intitiy, begins to shreak with a blood curtling sound.
You stand your ground, and banish it!''')
elif dice_value <= 10:
print ('''You watch in dispair, as the intity devours your friends.
You stand their, with no where to run, knowing that this is the end...''')
roll=raw_input('Roll Again?')
if roll == "no":
print ('Guess you could run too...')