简体   繁体   中英

Program ends at while loop

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!")

The program is just ending when it reaches the while loop. I have no idea why and how to fix this, any ideas? And i'm still unsure on how even to use loops properly, so i may be using it completely wrong, please let me know what im doing wrong and how to fix this.

You have a break in your while True: loop that is always executed:

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

Remove it or move it into an if statement.

Your next problem is that you never re-roll the dice in your loop. You'll need to keep assigning new random values to dice1 and dice2 for each round of the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM