【Python】随机数和 列表 python3随机数
off999 2024-12-20 17:55 38 浏览 0 评论
Python 随机模块方法
randint(a, b):
返回介于 a 和 b 之间(含两者)的随机整数。如果 a > b,这也会引发 ValueError。
import random
random_int = random.randint(1,10)
print(random_int)
>>
PS \Python> python .\random_int.py
4
PS \Python> python .\random_int.py
10
PS \Python> python .\random_int.py
3可以创建自己的模块并导入它。
#my_module.py
pi = 3.1415#random_int.py
import random
import my_module
random_int = random.randint(1,10)
print(random_int)
print(my_module.pi)
>>
PS \Python> python .\random_int.py
8
3.1415生成 Random 浮点数:
与生成整数类似,也有一些函数可以生成随机浮点序列。
- random.random() -> 返回介于 [0.0 到 1.0] 之间的下一个随机浮点数
- random.uniform(a, b) -> 返回一个随机浮点数 N,使得 a <= N <= b 如果 a <= b,b <= N <= a 如果 b < a。
- random.随机。expovariate(lambda) -> 返回与指数分布对应的数字。
- random.gauss(mu, sigma) -> 返回与高斯分布相对应的数字。
其他分布也有类似的函数,例如 Normal Distribution、Gamma Distribution 等。
import random
random_int = random.randint(1,10)
print(random_int)
random_flot = random.random()
print(random_flot)
>>
1
0.041635438063850505项目 1:爱情评分
import random
print("\n")
print("-------Welcome to Love Calculator-------\n")
name1 = input("What is your name?\n")
name2 = input("What is their name?\n")
love_score = random.randint(1, 100)
print(f"Love score of {name1} and {name2} is {love_score}")
>>
-------Welcome to Love Calculator-------
What is your name?
Lisa
What is their name?
Jack
Love score of Lisa and Jack is 34
项目 2:正面或反面
import random
random_side = random.randint(0, 1)
if random_side == 1:
print ("Heads")
else:
print("Tails")
>>
PS \Python> python.exe .\heads_tails.py
Tails
PS \Python> python.exe .\heads_tails.py
Heads列表:
检查此列表 : 数据结构
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits[2])
print(fruits[-2])
>>
pear
applefruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits[2])
print(fruits[-2])
fruits[2] = "Cheery" #CHNAGE NAME
print(fruits)
>>
pear
apple
['orange', 'apple', 'Cheery', 'banana', 'kiwi', 'apple', 'banana']可以将其附加到列表中:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.append("Nid")
print(fruits)
>>
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'Nid']扩展列表:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.extend(["AP", "gy"])
print(fruits)
>>
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'Nid', 'AP', 'gy'] 检查列表的长度
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(len(fruits))项目 3:谁来支付
names = input("Enter the names\n")
name_list = names.split(", ")
import random
num_item = len(names)
random_choice = random.randint(0, num_item - 1)
print(names[random_choice])嵌套列表
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
vegetables = ['Beetroot', 'Carrots', 'Capsicum', 'Ginger', 'Cauliflower']
fridge = [fruits, vegetables]
print(fridge)
>>
[['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'], ['Beetroot', 'Carrots', 'Capsicum', 'Ginger', 'Cauliflower']]项目 4:藏宝图
line1 = [" ", " "," "]
line2 = [" ", " "," "]
line3 = [" ", " "," "]
map = [line1, line2, line3]
print("\nHiding your treasure! X marks the spot.")
print("There are three rows and coloumes\n")
postion = input("Where do you want to put the treasure?")
letter = postion[0].lower()
abc = ["a", "b", "c"] #Possiable entry
letter_index = abc.index(letter)
#Checking if in "letter" (we entry), that is there in the list or not
#This will give us number
number_index = int(postion[1]) - 1
#Whatever position we entered, -1 because list stars from [0]
map[number_index][letter_index] = "X"
print(f"{line1}\n{line2}\n{line3}")
>>
Hiding your treasure! X marks the spot.
There are three rows and coloumes
Where do you want to put the treasure?B3
[' ', ' ', ' ']
[' ', ' ', ' ']
[' ', 'X', ' ']项目 5:石头剪刀布游戏
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
game_images = [rock, paper, scissors]
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"))
if user_choice >= 3 or user_choice < 0:
print("Number is Invalid, You lose!")
else:
print(game_images[user_choice])
computer_choice = random.randint(0, 2)
print("Compute Choose:")
print(game_images[computer_choice])
if user_choice == computer_choice:
print ("It's a tie!")
elif (user_choice == 0 and computer_choice == 2) or \
(user_choice == 1 and computer_choice == 0) or \
(user_choice == 2 and computer_choice == 1):
print ("You win!")
else:
print ("You lose!")
>>
What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.
2
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Compute Choose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
It's a tie!
相关推荐
- win10加快开机启动速度(加快开机速度 win10)
-
一、启用快速启动功能1.按win+r键调出“运行”在输入框输入“gpedit.msc”按回车调出“组策略编辑器”?2.在“本地组策略编辑器”依次打开“计算机配置——管理模块——系统——关机”在右侧...
-
- excel的快捷键一览表(excel的快捷键一览表超全)
-
Excel快捷键大全的一些操作如下我在工作中经常使用诸如word或Excel之类的办公软件。我相信每个人都不太熟悉这些办公软件的快捷键。使用快捷键将提高办公效率,并使您的工作更加轻松快捷。。例如,在复制时,请使用CtrI+C进行复制,...
-
2026-01-15 05:03 off999
- 华硕u盘启动按f几(华硕u盘装系统按f几进入)
-
F8。1、开机的同时按F8进入BIOS。2、在Boot菜单中,置secure为disabled。3、BootListOption置为UEFI。4、在1stBootPriority中usb—HD...
- 手机云电脑怎么用(手机云端电脑)
-
使用手机云电脑,您首先需要安装相应的云电脑应用。例如,华为云电脑APP。在安装并打开应用后,您将看到一个显示器的图标,这就是您的云电脑。点击这个图标,您将被连接到一个预装有Windows操作系统和必要...
- ie11浏览器怎么安装(ie11浏览器安装步骤)
-
如果IE浏览器11版本你发现无法正常安装,那么很可能是这样几个原因,一个就是电脑的存储空间不够到时无法安装,再有就是网络的问题,如果没有办法安装的话就不要再安装了,本身这个IE浏览器并不是多好用,你最...
- 台式机重装系统win7(台式机怎么重装win7)
-
下面主要介绍两种方法以重装系统:一、U盘重装系统准备:一台正常开机的电脑和一个U盘1、百度下载“U大师”(老毛桃、大白菜也可以),把这个软件下载并安装在电脑上。2、插上U盘,选择一键制作U盘启动(制作...
- 字母下划线怎么打出来(字母下的下划线怎么去不掉)
-
第一步,在电脑上找到文字处理软件WPS,双击即自动新建一个新文档。第二步,在文档录入需要处理的字母和数字,双击鼠标或拖动鼠标选择要处理的内容。第三步,在页面的左上方的横向菜单栏,找到字母U的按纽,点击...
- 怎么还原电脑上一次的设置(怎么还原电脑初始设置)
-
恢复出厂设置的方法如下:开机或者重启电脑按住DEL键,进入BIOS.这时有两个选项(一般在右边),一个是LoadFail-SafeDefaults既系统预设的稳定参数.另一个是LoadOp...
- wifi加密怎么设置(wifi加密怎么加密)
-
若你想将自己的无线网改成加密的,可以按照以下步骤操作:1.打开你的路由器管理界面。一般来说,在浏览器地址栏输入“192.168.1.1”或“192.168.0.1”,然后输入用户名和密码登录就可以打...
- 电脑怎么修改密码(惠普电脑怎么修改密码)
-
修改电脑的密码方法:第1步:点击电脑左下角的【开始】图标,然后点击“设置”;第2步:在“设置”界面中点击“账户”,然后点击“登录选项”;第3步:可以看到里面有各个类型的密码设置,选择你的密码类型点击它...
- win7修复计算机(win7修复计算机是什么意思)
-
就是系统的bug修复,简单说就是漏洞修复原因:电脑的系统文件出现损坏造成的。1、尝试开机,若开不了机,需要重启电脑。2、同时按下F8进入系统高级选项模式,选择最后一次正确配置来恢复系统。3、正常进入系...
- 不受限制的搜索浏览器(不受限制的搜索浏览器怎么解除)
-
华为手机不能直接控制浏览器搜索内容,但可以通过控制手机上的应用权限来间接控制。比如,您可以禁用某些应用程序的访问权限,以防止它们收集您的搜索数据。此外,您可以使用一些第三方应用程序来控制您的搜索内容,...
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
系统u盘安装(win11系统u盘安装)
-
Python 批量卸载关联包 pip-autoremove
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
