首页 > 编程知识 正文

第七次python上机作业(python上机总结)

时间:2023-12-24 12:05:20 阅读:320101 作者:MSNC

本文目录一览:

Python作业求助

就做一下标点符号的替换吧

txt = open("绝代风华.txt","r+",encoding='utf-8').read()#修改访问模式为"r+"

txt2=txt.replace(",",' ').replace("。",' ')#还有什么符号就自己加吧

txt3=open("E://绝代风华2.txt","w+",encoding='utf-8')#设置路径比较好,在文件名前加

txt3.write(txt2)#写入替换好的文本,形成新文本

#以下三句放在代码的后面

txt.close()

txt3.close()

#打开新文本,若查看的文本内容无误,这两行代码可以注释。

pl=open("E://绝代风华2.txt","r",encoding='utf-8').read()

pl.close()

python作业:用户从键盘输入一个长字符串以及要查询的单词,统计该单词出现的次数。

1、双击打开pycharm开发工具,创建python项目,然后新建python文件。

2、定义函数countNum,传入参数amn;然后使用字典,获取字符串中的字符,统计出现的次数,然后以字典形式返回。

3、定义一个字符串变量cmn,然后调用函数countNum,传入cmn,然后将返回的值给变量bmn,并打印。

4、再次定义一个空字符串变量kmn,调用字典的方法items();获取字符串以及对应出现的次数。

5、再次保存并运行python文件,查看控制台打印结果,以字符串形式展示。

python大作业的详细描述

# encoding=UTF-8  ==定义Python代码的编码为UTF-8

# 石头剪子布 程序

# 李忠

import random  ==导入随机生成器

 

# 定义石头剪子布字典

dict = {1:'剪子',2:'石头',3:'布'}  ==定义一个字典来保存数字和石头剪子布的对应关系

 

for row in dict:  ==遍历字典并在Console上面打印出数字和石头剪子布的关系

    print '编号:',row,' = ',dict[row]

 

print '您出什么?' 

 

loop = True   ==设置loop为True来让下面的while无限循环

while loop:  ==开始无限循环

    you = raw_input('请输入编号回车: ')  ==在Console打印提示

    try:  ==如果下面的代码出现异常就抛出异常

        you = int(you)  ==将用户输入的字符转换成int类型

        if you=1 and you=3:  ==如果你输入的数值大于1并且小于3就停止循环

            loop = False

        else:  ==否则继续循环并且打印以下提示

            print '请输入 1-3 范围内的编号'

    except Exception,e:  ==如果you = int(you)出现错误(异常)就输出下面的提示语

        print '请输入正确的数字编号'

 

dn = random.randint(1,3)  ==在1到3的范围内随机产生一个数字

print '你出:',dict[you]  ==打印用户输入数字所对应的出手类型

print '电脑出:',dict[dn]  ==打印计算机随机产生的数字对应的出手类型

print '结果:',

 

if dn==you:  ==如果计算机和用户的数值相同

    print '平局'

elif (youdn and you-dn==1) or you+2==dn:  ==如果用户输入的数值比计算机的随机数大1或者用户输入的数值比计算机的随机数小2

    print '你胜'

else:

    print '电脑胜'

Python 作业啊,求各位大虾帮忙。

answer 1:

def is_divisble(x, y):

if x % y == 0:

print('%s is divisible by %s'% (x, y))

return True

else:

print('%s is not divisible by %s'% (x, y))

return False

while True:

x = int(raw_input('Enter an integer : '))

y = int(raw_input('Enter another integer : '))

if is_divisble(x, y):

break;

answer 2:

def isPrime(d):

for i in range(2, d-1):

if d%i == 0:

return False

return True

integer = int(raw_input('Enter a number greater than 2: '))

result = 1

for j in range(2, integer):

if isPrime(j):

print j

result *= j

print(result if result 1 else 'null')

ps: about this question, you sure your example is right? 2 is odd number too.

answer 3:

def power(d):

return [d, d**2, d**3, d**4]

def display(a):

row = ''

for i in a:

s = str(i)

if len(s) 16:

s += ' ' * (16 - len(s))

row += s

print(row)

while True:

value = int(raw_input('Enter a number greater than 2: '))

if value 2:

display(['Number', 'Power of 2', 'Power of 3', 'Power of 4'])

display(['-------', '-------', '-------', '-------'])

for j in range(2, value, 2):

display(power(j))

break;

else:

print('Invalid. Number must be greater than 2.')

# all test success and , good luck

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。