Python骰子游戏问题

时间:2016-06-15 01:21:33

标签: python-3.x

我正在尝试学习更多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。

1 个答案:

答案 0 :(得分:1)

一些问题:

  1. 你正在调整I,它总是0或1:

    对于范围(1)中的i:

  2. 这将始终为0或1。 相反,您应该检查实际骰子卷,例如:

    dice_value=(random.randint(min,max))
    
    1. 使用原始输入而不是输入来获取字符串并使用“是”输入解决中心错误。
    2. 如果您有任何其他问题,请使用此代码:

      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...')