为什么我的phython骰子不断重复同一卷?

时间:2018-03-07 04:05:19

标签: python python-3.x

我是编程新手,我的教授希望我们写一个骰子游戏。起初它正在正常工作,现在它不断重复每卷的相同答案。请帮忙!

import random

turns = 0


dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
total = dice1 + dice2


while turns < 4:
    turns = turns + 1
    print("Presss enter to roll.")
    input()
    print("You rolled a {} and a {} ".format(dice1, dice2) + \
    "for a total of {}.".format(total))

    if total==7:
        print("Congratulations! You are a winner!!!")


    elif total==11:
        print("Congratulations! You are a winner!!!")

    elif dice1==dice2:
        print("Congratulations! You are a winner!!!")

2 个答案:

答案 0 :(得分:2)

你没有重新掷骰子移动random.randint in while循环

import random

turns = 0

while turns < 4:
    dice1 = random.randint(1, 6) // dice1 & dice2 should be assigned every iteration
    dice2 = random.randint(1, 6)
    total = dice1 + dice2
    turns = turns + 1
    print("Presss enter to roll.")
    input()
    print("You rolled a {} and a {} ".format(dice1, dice2) + \
    "for a total of {}.".format(total))

    if total==7:
        print("Congratulations! You are a winner!!!")

    elif total==11:
        print("Congratulations! You are a winner!!!")

    elif dice1==dice2:
        print("Congratulations! You are a winner!!!")

答案 1 :(得分:1)

您需要在循环的每次迭代中定义骰子。就像目前一样,骰子被定义一次,然后在每次迭代时重复使用。你的“按下输入”输入也有点时髦。您只需将消息放入输入法即可。这是一个有效的例子:

import random

turns = 0

while turns < 4:
  dice1 = random.randint(1, 6)
  dice2 = random.randint(1, 6)
  total = dice1 + dice2
  turns = turns + 1
  input("Press enter to continue.")
  print("You rolled a {} and a {} ".format(dice1, dice2) + \
  "for a total of {}.".format(total))

  if total==7:
      print("Congratulations! You are a winner!!!")

  elif total==11:
      print("Congratulations! You are a winner!!!")

  elif dice1==dice2:
      print("Congratulations! You are a winner!!!")
相关问题