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 time and random modules in python

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

Share

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

This article mainly introduces how to use time and random modules in python, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.

Mainly introduces the time,random module; do not memorize. First of all, let's talk about what a module is. Import time some friends, at first than confused, but later talk about classes (properties, methods), the package will be better, we did not talk so fast here, take your time, but in order to facilitate your memory. The simplest thing is. You know that x.py is the python script you wrote; if you define another time.py in the same directory. Your import time is called directly (this is also, we need to note that when defining x.py, the name of the module should not be the same as the name of the module that comes with it, or the name of the third-party module, unless you want to rewrite his module. (awesome) import time # # import time module, time.py file variables (properties); functions (methods) can be used. Import random # # Import random number module. Print (time.time ()) # print the current timestamp. How to convert? Let's do a search. Time.sleep (3) # sleep for 3 seconds print (random.randint (1100)) # does it print an integer from 1 to 100? Number = range (1100) # number becomes a list. Print (random.sample (number,3)) # did you draw 3 digits? # Let's simulate a fight. People_a_life = random.randint (150200) people_b_life = random.randint (150200) people_a_kill_power = random.randint (50Magazine 80) people_b_kill_power = random.randint (50Magazine 80) print ("goons 1\ nBlood volume:% s lethality:% s"% (people_a_life People_a_kill_power)) time.sleep (2) print ("goons 2\ nBlood volume:% s"% (people_b_life,people_b_kill_power)) while people_a_life > = 0 and people_b_life > = 0: people_b_life = people_b_life-people_a_kill_power print ("start goons 1 * *:% s" Goons 2 people_a_life% s "% (people_a_kill_power,people_b_life)) time.sleep (2) people_a_life = people_a_life-people_b_kill_power print (" start goons 1 * *:% s, goons 2% s "% (people_b_kill_power) People_a_life) time.sleep (1) if people_a_life > people_b_life: print ("goons 1 win") elif people_b_life > people_a_life: print ("goons 2 win") else: print ("draw") Thank you for reading this article carefully I hope the article "how to use time and random modules in python" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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