Code replaying?

时间:2017-04-10 00:49:40

标签: python loops

My code seems to be replaying a point where there is 2 roll variables (Roll1 and Roll2) it then prints the Roll1 and Roll2 but when i run the program it runs this part of the code 2-3 times, i only want it to play once. what am i doing wrong, this is using python i am a student and very new to this, marked with comments in code

from random import randint
print("Welcome")
print("You are are playing a game of dice, this is a two player game, who  ever rolls the highest number wins")
name1 = input("What is Player 1's Name?")  
print("Hello", name1)
name2 = input("What is Player 2's Name?")
print("Greetings",name2)
condition = "Start"
while condition == "Start":
     Dice = int(input("Please select number of dice you would like to use between 1 and 5"))
    if (Dice >= 1 and Dice <= 5):
        Sides = int(input("Please select number of sides on dice you would like to use"))
    if (Sides >= 2 and Sides <= 5):
    if (Dice >=0 and Sides >= 1):
        condition = "start"
    print("You have selected", Dice," dice and", Sides,"Sides")
    condition = "play"
    Roll1 = 0
    Roll2 = 0
    Player1 = 0
    Player2 = 0
while condition == "play":
    for i in range(Dice):
        Roll1 += randint(1,Sides) #The code here
        Roll2 += randint(1,Sides)
        print (name1,"'s Roll", Roll1)
        print (name2,"'s Roll", Roll2) #To Here
    if Roll1 > Roll2:
        print (name1," wins")
        Player1 += 1
    elif Roll2 > Roll1:
        print (name2," wins")
        Player2 += 1
    elif Roll1 == Roll2:
        print ("It is a draw no points this round")
    if(input("Press 'Enter' to play again or type 'reveal' to reveal the scores and quit") == "reveal"):
        print ("The Score for",name1,"is",Player1,)
        print ("The Score for",name2,"is",Player2,)
        if(input("Press 'Enter' to goto the start or 'quit' to quit the game") =="quit"):
            print ("Goodbye")
            break
    else:
        condition == "Start"

1 个答案:

答案 0 :(得分:0)

I believe your problem is the way your for loop is working. I've added some comments.

for i in range(Dice): # this means you'll 'roll' Dice times.
    Roll1 += randint(1,Sides) # This is fine.
    Roll2 += randint(1,Sides)
print (name1,"'s Roll", Roll1) # these statements need to be outside the loop
print (name2,"'s Roll", Roll2) # because you only want to print once

I'm assuming you want the roll to be the cumulative total of all their rolls.