#! /usr/bin/env python3 # def jeopardy ( filename = 'names.txt' ): #*****************************************************************************80 # ## jeopardy() prints a random first name from a class roster file. # # Discussion: # # Pick a student, by first name, from a class roster. # # The class roster is assumed to be a plain text file. # Each line lists one student. # The line lists the student's Pitt ID, first name, last name. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 January 2023 # # Author: # # John Burkardt # # Input: # # string filename: the name of the class roster file. # import numpy as np # # We will create a list of names in the list "name". # Initialize it as an empty list, and set the length n to 0. # n = 0 name = [] # # Open the roster file. # try: input = open ( filename, 'r' ) except: print ( 'jeopardy(filename) could not open "' + filename + '"' ) return # # Read each line, split it into words, and copy the second word into "name". # for line in input: words = line.split() name.append ( words[1] ) n = n + 1 input.close ( ) # # Pick a random integer n: 0 <= r < n # r = np.random.randint ( low = 0, high = n ) # # Print the student first name from record r. # print ( 'Python Jeopardy winner is #' + str ( r ) + ': ' + name[r] + '!' ) return if ( __name__ == "__main__" ): jeopardy ( 'math1800_roster.txt' )