百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

【Python】随机数和 列表 python3随机数

off999 2024-12-20 17:55 37 浏览 0 评论



Python 随机模块方法

randint(a, b):

返回介于 ab 之间(含两者)的随机整数。如果 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
apple
fruits = ['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!

相关推荐

软件商店下载官方网站(软件商店正版软件下载)

软件商店安装的方法步骤如下:1.第一步,需要注册一个微软账户,然后点击桌面左下角的开始图标,然后在开始菜单中找到微软商店图标,点击进入。2.第二步,点击进入应用商店主页。3.第三步,在商店中搜索...

系统应用架构(系统应用架构有哪些)

一、目的不同:系统架构是对已确定的需求的技术实现构架、作好规划,运用成套、完整的工具,在规划的步骤下去完成任务。应用构架是描述了IT系统功能和技术实现内容的构架。二、实现方式不同:系统架构通过规划程序...

雨林木风ghostxpsp3纯净版(雨林木风xp系统怎么样)

1.你下载的雨林木风GHOSTXPSP3纯净版Y8.0是一个克隆光盘映像文件,首先将其刻录成光盘,这个光盘是一个带有启动系统的系统克隆安装光盘;2.将电脑设置成光驱启动(在启动电脑时连续按DEL键...

加密u盘怎么解除(加密的u盘如何解除)

1、打开控制面板,修改查看方式,点击bitlocker驱动器加密选项2、在新窗口点击地下的bitlocker驱动器加密的解锁驱动器3、在弹出的窗口中键入解锁密码,点击解锁4、然后在刚刚的窗口中点击...

itunes下载的固件在哪个位置

可以刷机的将手机联入电脑电脑会自动下载符合手机的版本的固件是一样的,都是通过官方固件包来重装系统。操作步骤如下;1.电脑端下载最新版本的itunes,不然重装时可能会造成未知错误。2.iphone...

电脑重装系统后没声音(电脑重装系统后没声音怎么解决win7)

如果您在电脑重装系统后没有声音,可能是由于以下一些常见问题导致的:1.驱动程序问题:重装系统后,可能需要重新安装声卡驱动程序。您可以从电脑制造商的官方网站或声卡制造商的官方网站上下载并安装最新的声卡驱...

win10制作系统u盘(制作win10系统优盘)

方法一:使用微软官方工具制作u盘工具安装win101、首先电脑浏览器输入“windows10下载”,找到微软官方地址进入,然后选择立即下载工具到电脑上。2、鼠标右键选择以管理员身份运行,同意协议进入下...

苹果手机wlan设置在哪(苹果手机 wlan)

进入设置->Wi-Fi。如何设置iPhone的WIFI?2.选择Wi-Fi之后,会显示附近能搜索到的所有的Wi-Fi网络。如何设置iPhone的WIFI?3.选中其中网络Wi-F...

召唤系统游戏(召唤系统游戏排行)

亡灵进化专家:写的很不错了。猪脚可以用金属或骨头帮自己的亡灵进化升级挺有意思的。不过还没写完网游之审判:是英雄无敌类型的。不过写的很牛逼也写完了。推荐看看还有不死传说:虽然不是召唤的,主教是僵尸和吸血...

w7系统怎么样(电脑w7系统怎么样)

有以下几点理由来分析为什么win7受欢迎1、Windows7有望受到企业用户认可微软目前的最大担心是:企业用户认为Windows7性能同Vista相差不大,因此不会出手购买。微软当初发布Vist...

无敌系统流小说(无敌系统流的小说)

《嫡女之花开富贵》作者:伊人睽睽简介祖父是镇国将军,贵不可言;外公是帝师,才名满天下;父母亲琴瑟和鸣,恩爱无双,无妾室插足;穿越为书香门第的嫡小姐,且无任何庶兄妹,慕兰音认为,她这一生,必将佳期如梦...

键盘上windows键是哪个键(电脑键盘上windows键是哪个)
键盘上windows键是哪个键(电脑键盘上windows键是哪个)

一、台式机键盘。Windows键,简称“Winkey”或“Win键”,是在计算机键盘左下角Ctrl和Alt键之间的按键,台式机全尺寸键盘的主键盘区左下角和右下角各有一个,图案是MicrosoftWindows的视窗徽标。二、笔记...

2026-01-13 11:51 off999

笔记本电脑必备app(笔记本电脑必备管宿)
  • 笔记本电脑必备app(笔记本电脑必备管宿)
  • 笔记本电脑必备app(笔记本电脑必备管宿)
  • 笔记本电脑必备app(笔记本电脑必备管宿)
  • 笔记本电脑必备app(笔记本电脑必备管宿)
桌面图标设置在哪打开(桌面图标从哪里调出)
桌面图标设置在哪打开(桌面图标从哪里调出)

1、首先来到电脑桌面,此时桌面没有任何图标,如下图所示。2、我们先右键单击任务栏,会出现工具栏,这时我们在下拉的选项里选择“快速启动”按钮。3、单击快速启动按钮后会出现如图所示情况,这时在电脑屏幕的左下方会显示很多快捷按钮,一般情况下单击快...

2026-01-13 10:51 off999

windows如何进入启动项(怎么进入启动选项)

方法步骤如下:1.点击应用在Windows设置界面点击应用选项进入。2.选择启动在左侧分类中选择启动选项。3.点击开关点击软件后方的开关即可启动或关闭开机启动项。1、在Window的文件资...

取消回复欢迎 发表评论: