how to tell an infinite loop to end once one number repeats twice in a row (in python 3.4)

2024/11/9 3:51:37

The title says it all. I have an infinite loop of randomly generated numbers from one to six that I need to end when 6 occurs twice in a row.

Answer

The following is a working example. With comments in the code so you can better understand each step.

# import required to use randint
import random# holds the last number to be randomly generated
previous_number = None
while True: # infinite loop# generates a random number between 1 and 6num = random.randint(1, 6)# check if the last number was 6 and current number is 6if previous_number == 6 and num == 6:# if the above is true then break out the loopbreak# store the latest number and start the loop againprevious_number = num 
https://en.xdnf.cn/q/120534.html

Related Q&A

Is it possible to customize the random function to avoid too much repetition of words? [duplicate]

This question already exists:Customizing the random function without using append, or list, or other container?Closed last year.Theoretically, when randomization is used, the same word may be printed …

country convert to continent

def country_to_continent(country_name):country_alpha2 = pc.country_name_to_country_alpha2(country_name)country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)country_continent_name…

Iterate through a list and delete certain elements

Im working on an assignment in my computer class, and Im having a hard time with one section of the code. I will post the assignment guidelines (the bolded part is the code Im having issues with):You a…

How to count numbers in a list via certain rules?

Just to say I have a str and a list of strs and I want to count how many strs in the list that is contained in a str. Is there an elegant way to do that?For example, l = {"foo", "bar&qu…

Count character occurrences in a Python string

I want to get the each character count in a given sentence. I tried the below code and I got the count of every character but it displays the repeated characters count in the output. How to delete repe…

The Concept Behind itertoolss product Function

so basically i want to understand the concept of product() function in itertools. i mean what is the different between yield and return. And can this code be shorten down anyway.def product1(*args, **k…

how to read data from multiple sheets in a single csv file using python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Quality Center: Set a Step Field in Python

I have a very simple problem here. I want to achieve the following VB script Code in Python:- dim objSfact dim objOrun dim mystep Set objOrun = QCutil.CurrentRun Set objSfact = objOrun.StepFactory…

Convert for loop from Python to C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Storing values in a CSV file into a list in python

Id like to create a list that stores all values of a single column. For example lets say my file has a column called FirstNames and for the first 3 rows, the names column has Merry, Pippin, Frodo.Id l…