# python习题及答案

11 篇文章 13 订阅

作业四

1、 编写一个从 1 加到 end 的当型循环。变量 end 的值由键盘输入。假如输入 end

sum 作为变量，因为它是内置函数）。

a = input()
b=int(a)
total = 0
for i in range(b+1):
total = i+total
print(total)


2、从键盘输入一个整数，判断该数字能否被 2 和 3 同时整除，能否被 2 整除，能否被 3 整除，不能被 2 和 3 整除。输出相应信息。

a = input()
b=int(a)
if b%2==0 and b%3==0:
print('该数字能被 2 和 3 同时整除')
elif b%2==0:
print('该数字能被 2 整除')
elif b%3==0:
print('该数字能被 3 整除')
else :
print('该数字不能被 2 和 3 整除')

3、 一个数如果恰好等于它的因子之和，这个数就称为“完数”，例如， 6 的因子

6 its factors are 1, 2, 3











for i in range(1001): if i==0: continue s = 0 for j in range(i): if j==0: continue if i%j==0: s=s+j if s==i: print('{} its factors are '.format(i),end='') for j in range(i): if j == 0: continue if i % j == 0: print(j,end=' ') print('\n')

4、打印出所有的“水仙花数”，所谓“水仙花数”是指一个 3 位数，其各位数字立方和等于该数本身。例如， 153 是一个水仙花数，因为 153=13+53+33

for i in range(1000):
if i<100:
continue
s = 0
a = int(i//100)
b = int(i//10%10)
c = int(i%10)
s = a**3+b**3+c**3
if s == i:
print("{} 是水仙花数".format(i))


j=0
for i in range(101):  #循环
if i>0 and i % 7 == 0: #如果能整除7，则再执行循环
continue
elif i>0:    #如果大于0并且不能被7整除，输出；
print("{:3d}".format(i), end=' ')
j += 1   #j用来控制每行输出是否是10个
if j % 10 == 0:
print('\n')
j = 0
else:
continue;

Very quietly I take my leave

As quietly as I came here;

Quietly I wave good-bye

To the rosy clouds in the western sky.

The golden willows by the riverside

Are young brides in the setting sun;

Their reflections on the shimmering waves

Always linger in the depth of my heart.

The floating heart growing in the sludge

Sways leisurely under the water;

In the gentle waves of Cambridge

I would be a water plant!

That pool under the shade of elm trees

Holds not water but the rainbow from the sky;

Shattered to pieces among the duckweeds

Is the sediment of a rainbow-like dream?

To seek a dream? Just to pole a boat upstream

To where the green grass is more verdant;

Or to have the boat fully loaded with starlight

And sing aloud in the splendor of starlight.

But I cannot sing aloud

Quietness is my farewell music;

Even summer insects heap silence for me

Silent is Cambridge tonight!

file = open('C:/Users/uestc2020/Desktop/hanzhengqiang.txt','w')
#在桌面新建一个韩正强.txt文本文档，并且是写的模式
#在文本中写入文字：
file.write('''Very quietly I take my leave
As quietly as I came here;
Quietly I wave good-bye
To the rosy clouds in the western sky.
The golden willows by the riverside
Are young brides in the setting sun;
Their reflections on the shimmering waves
Always linger in the depth of my heart.
The floating heart growing in the sludge
Sways leisurely under the water;
In the gentle waves of Cambridge
I would be a water plant!
That pool under the shade of elm trees
Holds not water but the rainbow from the sky;
Shattered to pieces among the duckweeds
Is the sediment of a rainbow-like dream?
To seek a dream? Just to pole a boat upstream
To where the green grass is more verdant;
Or to have the boat fully loaded with starlight
And sing aloud in the splendor of starlight.
But I cannot sing aloud
Quietness is my farewell music;
Even summer insects heap silence for me
Silent is Cambridge tonight!''')


file = open('C:/Users/uestc2020/Desktop/hanzhengqiang.txt','r')
#在桌面打开一个韩正强.txt文本文档，并且是读的模式
#读取文件，并且输出：
print(s)

1） 读入该文件，统计并输出单词的个数

2） 返回单词列表（注意：指返回单词列表即可，不要在屏幕上显示）

import re
#统计单词个数：
def count_words(file_path):
with open(file_path) as file:
words = re.findall(r'[a-zA-Z]+', text)
count = len(words)
return count
print(count_words('C:/Users/uestc2020/Desktop/words.txt'))


1、 被零除时，输出"division by zero! "；

2、 类型不一致时，强制转换为整数再调用本函数；

3、 若没有上述异常则输出计算结果。

def test(password, earning, age):
#assert 1<0
assert int(earning)>=0 and int(earning)<=20000
assert int(age)>=18 and int(age)<=70
return True
print(test('as','11','30'))

def test(password, earning, age):
#assert 1<0
assert int(earning)>=0 and int(earning)<=20000
assert int(age)>=18 and int(age)<=70
return True


1、编程实现如下GUI（只显示，没有回调函数）

import tkinter

root = tkinter.Tk()
root.title('tk')
theLabel=tkinter.Label(root,text='Welcome to Python Tkinter')
theLabel.pack()
root.geometry('500x300+300+300')
button = tkinter.Button(root,text='Click Me')
button.pack()
root.mainloop()


2、 编程响应用户事件。“OK”按钮前景为红色，按下“OK”按钮显示"OK button is clicked"，”Cancel“按钮背景为黄色，按下”Cancel“按钮显示"Cancel button is clicked"。

import tkinter

def ok_on_click():
print('OK button is clicked')
def cancel_on_click():
print('Cancel button is clicked')
root = tkinter.Tk()
root.title('zy')
label = tkinter.Label(root)
buttonok = tkinter.Button(root,text='OK',fg='red')
buttonok['command']=ok_on_click
label.pack()
buttonok.pack()
root.geometry('500x300+300+300')
buttoncancel = tkinter.Button(root,text='Cancel',bg='yellow')
buttoncancel['command']=cancel_on_click
label.pack()
buttoncancel.pack()
root.mainloop()


1、绘制一个红色的五角星图形，如图所示。

import  turtle as tk
tk.fillcolor("red")
tk.begin_fill()
while True:
tk.forward(200)
tk.right(144)
if abs(tk.pos()) < 1:
break
tk.end_fill()
input()


2、螺旋线绘制（每一圈是四个线段组成，一共绘制100个线段）。绘制一个螺旋线的图形，如图所示。

import turtle
import time
turtle.pensize(1)
for x in range(100):
turtle.forward(x)
turtle.left(90)
time.sleep(100)

• 66
点赞
• 615
收藏
觉得还不错? 一键收藏
• 5
评论
01-03
04-16 4997
04-05 831
01-08
12-23 2万+
08-11
04-06
07-14
12-26
11-12
03-14
11-22
03-12

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。