四时宝库

程序员的知识宝库

Python自学编程习题笔记,让电脑掷色子如何(二)

今天习题任务

一、Python 平方根

教程网址https://www.runoob.com/python3/python3-square-root.html

def sq(a):
    c=a**0.5
    return c
a=float(input('请输入:',))
print('数字 {0} 的平方根: {1}'.format(a, sq(a)))

二、Python 随机丢色子,看看数据如何

import random
for i in range(1,21):
    a=random.randint(1,6)
print('随机数为',a)

我们使用了 random 模块的 randint() 函数来生成随机数,randint函数包含括号内两边的数.

又将程序完善了下,加了序列,特别需要提醒的是,i 需要指定是不是字符串,用str(i)定义好

import random
for i in range(1,21):
    a=random.randint(1,6)
    print(str(i)+'.随机数为',a)

三、Python 建个文本文件,读取文本文件

with open('t.txt','wt') as myfile:
    myfile.write('程序测试')


 
with open('t.txt','wt') as myfile:
    myfile.write('程序测试')
with open('t.txt','rt') as readfile:
    text=readfile.read()
print(text)
 

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接