python - 程序在while循环处结束

标签 python

import random


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


strengthone = int(input ("Player 1, between 1 and 10 What do you want your characters strength to be? Higher is not always better."))
skillone = int(input ("Player 1, between 1 and 10 What do you want your characters skill to be? Higher is not always better."))

if strengthone > 10:
    print ("Incorrect value")
else:
    print ("Good choice.")
if skillone > 10:
    print ("Incorrect value.")
else:
    print ("Good choice.")

strengthtwo = int(input ("Player 2, between 1 and 10 what do you want your characters strength to be? Higher is not always better."))
skilltwo = int(input ("Player 2, between 1 and 10 what do you want your characters skill to be? Higher is not always better."))

if strengthtwo > 10:
    print ("Incorrect value.")
else:
    print ("Good choice.")
if skillone > 10:
    print ("Incorrect value.")
else:
    print ("Good choice.")

strengthmod = strengthone - strengthtwo
skillmod = skillone - skilltwo
strengthmodone = strengthone - strengthtwo
skillmodone = skillone - skilltwo
strengthmodtwo = strengthone - strengthtwo
skillmodtwo = skillone - skilltwo

print ("Player 1, you rolled a", str(dice1))
print ("Player 2, you rolled a", str(dice2))

while True:
    if dice1 == dice2:
        print ("")
    if dice1 > dice2:
        strengthmodone = strengthmod + strengthone
        strengthmodone = strengthmod + strengthone
    if dice2 > dice1:
        strengthmodtwo = strengthmod + strengthtwo
        skillmodtwo = skillmod + skilltwo
    if dice1 < dice2:
        strengthmodone = strengthmod - strengthone
        skillmodone= skillmod - skillone
    if dice2 < dice1:
        strengthmodtwo = strengthmod - strengthtwo
        skillmodtwo = skillmod - skilltwo
    break
    if strengthmodone == 0:
        print ("Player one dies, well done player two. You win!")
    if strengthmodtwo == 0:
        print ("Player two dies, well done player one. You win!")
    if strengthmodone== 0:
        print ("Player one dies, well done player two. You win!")
    if strengthmodtwo == 0:
        print ("Player two dies, well done player one. You win!")

当程序到达while循环时,程序就结束了。我不知道为什么以及如何解决这个问题,有什么想法吗?而且我仍然不确定如何正确使用循环,所以我可能完全错误地使用它,请让我知道我做错了什么以及如何解决这个问题。

最佳答案

您的 while True: 循环中有一个break,它总是执行:

while True:
    if dice1 == dice2:
        # [ ... ]
    # [ ... ]
    # more `if` statements
    # [ ... ]
    if dice2 < dice1:
        # [ ... ]
    break

删除它或将其移动到 if 语句中。

你的下一个问题是你永远不会在循环中重新掷骰子。您需要在游戏的每一轮中不断为 dice1dice2 分配新的随机值。

关于python - 程序在while循环处结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19954095/

相关文章:

Python-将一个时间段分割成多个固定长度的时间段

python - 如何在不保存图像的情况下将 PIL Image 对象上传到 Discord 聊天?

python - 简单但奇怪的 vstack/concatenate 问题 (python)

python - 创建可重用的 Django 应用程序?

python - 使用 matplotlib 填充行与行之间的空间

Python 模块未找到错误 : No module named 'azure.cognitiveservices'

python - 关于请求 url 响应 404,但在浏览器中成功并抓取标签的问题

python - 将终端输出重定向到 tkinter

python - 用于设置 xtics 的 mpld3 插件

python - 如何从 enaml.stdlib.sessions 导入 show_simple_view?