简体   繁体   中英

How to implement a Loop statement

I made a simple Yahtzee game in as few of lines as I could think. Currently, the user must press Enter (with any value) to continue. I would like to use a loop statement so that the dice continue to roll until Yahtzee (all rolled numbers are the same). I would also like a 10 second timer. What is the best way to add a loop statement to this code? PS This is not homework, I wanted to make this game for my Yahtzee nights. My daughter wakes easil...haha

import random

while True:
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    dice3 = random.randint(1,6)
    dice4 = random.randint(1,6)
    dice5 = random.randint(1,6)
    numbers = (dice1, dice2, dice3, dice4, dice5)
    sum1 = sum(numbers)
    if  sum1 == ("5" or "10" or "15" or "20" or "25"):
        print("Winner, winner, chicken dinner!", dice1, dice2, dice3, dice4, dice5)
    else:
        print("Your rolls are: ", dice1, dice2, dice3, dice4, dice5)
    input("Press return key to roll again.")

EDIT: This is my final product. Thank you for all the help guys!!

import random
import time
input("Press return key to roll.")
for x in range(0,10000):
    numbers = [random.randint(1,6) for _ in range(5)]
    if all(x == numbers[0] for x in numbers):
        print("Winner, winner, chicken dinner!", numbers)
        input("Press return key to play again.")

    else:
        print("Your rolls are: ", numbers)        
        print("Next roll in one second.")
        time.sleep(1)   

Replace the while True:... with a while boolean_variable: ... and set the value of that boolean_variable to True before the while loop and to False when you achieved Yahtzee => in the if condition.

What do you mean by 10 second timer, however? A sleep time of ten seconds can be implemented by a time.sleep(10) at the end of the inner while loop.

EDIT boolean_variable EXAMPLE

import time
...
not_yahtzee = True
while not_yathzee:
    ....
    if sum == 5 or sum == 10 or ... :
        ...
        not_yahtzee = False
    else:
        ...
        time.sleep(10)

The ... represent the code you already have. As commented on your question, the if condition should look more like this one. There are other ways on how to check all the elements in a list are the same .

If you would like to check if all the dice numbers are the same it is as simple as.

allDice = [dice1, dice2, dice3, dice4, dice5] #List of dice variables
if all(x == allDice[0] for x in allDice): # If all dice are the same
    print("Yahtzee")
    break # Break out of while loop

The most simple way of having a "timer" could be adding a time.sleep() . You have to import time otherwise it won't work.

So replace input("Press return key to roll again.") with time.sleep(10)

This means every 10 seconds the dice will roll until all dice are the same number, if they are then it prints Yahtzee and stops the loop.

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