Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to make a random roll call program

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use python to do a random roll call program". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Import random # Import Random Module import pyttsx3 # Import third Party Voice Module listen = pyttsx3.init () # initialize a variable txt_path = input ('Please enter the path:' of the txt file) # the path to the list file entered by the user (the list file is txt, there can be only one name per line The name must have no spaces at the beginning of the line) txt = open (f'{txt_path}','r') Encoding='utf-8') # Open the list file name_list = [] # create an empty list to store the list for i in txt: # for cycle through I = i.replace ('\ n' '') # process string to remove the newline character name_list.append (I) # add to the name list print (name_list) # print list txt.close ( ) # close the list file while True: # Yongzhen infinite loop if len (name_list) = = 0: # when Everyone on the list is clicked once and ends with print ('end') break r_num = random.randint (0 Len (name_list)-1) # create a random number a = input ('enter b to exit Other values continue:') # Let the user choose whether to continue the roll call or exit the roll call if a = 'baked: # exit print (' end') break else when the user enters b to exit: # otherwise start random roll call print (f 'this time is called: {name_ list [r _ num]}') listen.say (f'{name_ list [r _ num]}') # output name voice listen.runAndWait () # run language Name_list.pop (r_num) # removes overnamed ones to prevent repetition of print () # interval effect

Divide the code into four parts

I. Import module

Need two modules, a built-in module and a third-party module are: random, pyttsx3 (this is the third release module, need to be installed).

Import randomimport pyttsx3 II. The user enters the list file and uses the program to process txt_path = input ('please enter the path:' of the txt file) # the path to the list file entered by the user (the list file is txt, and there can be only one name per line) The name must have no spaces at the beginning of the line) txt = open (f'{txt_path}','r') Encoding='utf-8') # Open the list file name_list = [] # create an empty list to store the list for i in txt: # for cycle through I = i.replace ('\ n' '') # process string to remove the newline character name_list.append (I) # add to the name list print (name_list) # print list txt.close ()

The user wants to enter the path of the list file, for example

At this time, let's run the program and enter the path of the list to see the effect.

Third, use the pyttsx3 module listen = pyttsx3.init () listen.say ('output voice') listen.runAndWait () # to start outputting voice

Note that the pyttsx3 module needs to be downloaded by yourself, which can be downloaded by typing the command pip install pyttsx3 in cmd.

4. Start at random while True: # Yongzhen infinite loop if len (name_list) = = 0: # end print ('end') break r_num = random.randint (0) when everyone on the list is clicked once Len (name_list)-1) # create a random number a = input ('enter b to exit Other values continue:') # Let the user choose whether to continue the roll call or exit the roll call if a = 'baked: # exit print (' end') break else when the user enters b to exit: # otherwise start random roll call print (f 'this time is called: {name_ list [r _ num]}') listen.say (f'{name_ list [r _ num]}') # output name voice listen.runAndWait () # run language Name_list.pop (r_num) # removes overnamed ones to prevent repetition of print () # interval effect

Random roll call is based on the creation of a random number and then the list is based on the random number index to achieve a random effect.

This is the end of the content of "how to use python to make a random roll call program". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report