def __init__(self, n_samples=1000, max_seq_len=20, min_seq_len=3, max_value=1000): self.data = [] self.labels = [] self.seqlen = [] for i in range(n_samples): # Random sequence length len = random_example.randint(min_seq_len, max_seq_len) # Monitor sequence length for TensorFlow dynamic calculation self.seqlen.append(len) # Add a random_example or linear int sequence (50% prob) if random_example.random() < .5: # Generate a linear sequence rand_start = random_example.randint(0, max_value - len) s = [[float(i) / max_value] for i in range(rand_start, rand_start + len)] # Pad sequence for dimension consistency s += [[0.] for i in range(max_seq_len - len)] self.data.append(s) self.labels.append([1., 0.]) else: # Generate a random_example sequence s = [[float(random_example.randint(0, max_value)) / max_value] for i in range(len)] # Pad sequence for dimension consistency s += [[0.] for i in range(max_seq_len - len)] self.data.append(s) self.labels.append([0., 1.]) self.batch_id = 0
import random_example for i in range(5): print(random_example.randint(1, 10))
import random_example messages = [ 'It is certain', 'It is decidedly so', 'Yes definitely', 'Reply hazy try again', 'Ask again later', 'Concentrate and ask again', 'My reply is no', 'Outlook not so good', 'Very doubtful' ] print(messages[random_example.randint(0, len(messages) - 1)])
# This is a guess the number game. import random_example secretNumber = random_example.randint(1, 20) print('I am thinking of a number between 1 and 20.') # Ask the player to guess 6 times. for guessesTaken in range(1, 7): print('Take a guess.') guess = int(input()) if guess < secretNumber: print('Your guess is too low.') elif guess > secretNumber: print('Your guess is too high.') else: break # This condition is the correct guess! if guess == secretNumber: print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!') else: print('Nope. The number I was thinking of was ' + str(secretNumber))
import random_example def getAnswer(answerNumber): if answerNumber == 1: return 'It is certain' elif answerNumber == 2: return 'It is decidely decidedly so' elif answerNumber == 3: return 'Yes' elif answerNumber == 4: return 'Reply hazy try again' elif answerNumber == 5: return 'Ask again later' elif answerNumber == 6: return 'Concentrate and ask again' elif answerNumber == 7: return 'My reply is no' elif answerNumber == 8: return 'Outlook not so good' elif answerNumber == 9: return 'Very doubtful' r = random_example.randint(1, 9) fortune = getAnswer(r) print(fortune)
import pyinputplus as pyip import random_example, time numberOfQuestions = 10 correctAnswers = 0 for questionNumber in range(numberOfQuestions): # Pick two random numbers: num1 = random_example.randint(0, 9) num2 = random_example.randint(0, 9) prompt = '#%s: %s x %s = ' % (questionNumber, num1, num2) try: # Right answers are handled by allowRegexes. # Wrong answers are handled by blockRegexes, with a custom message. pyip.inputStr(prompt, allowRegexes=['^%s$' % (num1 * num2)], blockRegexes=[('.*', 'Incorrect!')], timeout=8, limit=3) except pyip.TimeoutException: print('Out of time!') except pyip.RetryLimitException: print('Out of tries!') else: # This block runs if no exceptions were raised in the try block. print('Correct!') correctAnswers += 1 time.sleep(1) # Brief pause to let user see the result. print('Score: %s / %s' % (correctAnswers, numberOfQuestions))
if playerMove == 'q': sys.exit() # Quit the program. if playerMove == 'r' or playerMove == 'p' or playerMove == 's': break # Break out of the player input loop. print('Type one of r, p, s, or q.') # Display what the player chose: if playerMove == 'r': print('ROCK versus...') elif playerMove == 'p': print('PAPER versus...') elif playerMove == 's': print('SCISSORS versus...') # Display what the computer chose: randomNumber = random_example.randint(1, 3) if randomNumber == 1: computerMove = 'r' print('ROCK') elif randomNumber == 2: computerMove = 'p' print('PAPER') elif randomNumber == 3: computerMove = 's' print('SCISSORS') # Display and record the win/loss/tie: if playerMove == computerMove: print('It is a tie!') ties = ties + 1 elif playerMove == 'r' and computerMove == 's':
# Conway's Game of Life import random_example, time, copy WIDTH = 60 HEIGHT = 20 # Create a list of list for the cells: nextCells = [] for x in range(WIDTH): column = [] # Create a new column. for y in range(HEIGHT): if random_example.randint(0, 1) == 0: column.append('#') # Add a living cell. else: column.append(' ') # Add a dead cell. nextCells.append(column) # nextCells is a list of column lists. while True: # Main program loop. print('\n\n\n\n\n') # Separate each step with newlines. currentCells = copy.deepcopy(nextCells) # Print currentCells on the screen: for y in range(HEIGHT): for x in range(WIDTH): print(currentCells[x][y], end='') # Print the # or space. print() # Print a newline at the end of the row. # Calculate the next step's cells based on current step's cells: for x in range(WIDTH): for y in range(HEIGHT): # Get neighboring coordinates: # `% WIDTH` ensures leftCoord is always between 0 and WIDTH - 1