欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python学习日记三(20180828)

程序员文章站 2024-02-03 22:27:34
...

1.学习笔记
Python学习日记三(20180828)

2.检查是否是素数

n=input('enter the number:')
number=int(n)
i=2
while i<number:
    if number%i==0:
        j=True
        break
    else:
        i=i+1
        j=False

if j==True:print(n+' is\'t the prime number')
else: print(n+' is the prime number')

3.检查是否是回数

numbers=input('Plz enter the number U want to test:')
number_x=list(numbers)#将字符串转换为列表
number_y=number_x.copy()
number_y.reverse()#将复制好的列表y反向排列
length=0
value=True #取一个Bealen数,为之后判断做准备
while length<len(number_x):
    if int(number_x[length])==int(number_y[length]):#将number_x的每一列字符整数后与number_y的每一列字符整数后比较
        length=length+1            
    else:
        value=False
        break

if value==True:print(numbers+' is a palindrom')
else: print(numbers+' is\'t a palindrom')

4.在中间显示句子

sentence=input('enter the sentence:')
screen_width=80
text_width=len(sentence)
box_width=text_width+6
left_margin=(screen_width-text_width)//2

print()
print(' '*left_margin+'+'+'-'*(box_width-2)+'+')
print(' '*left_margin+'|'+' '*(box_width-2)+'|')
print(' '*left_margin+'|'+' '*2+sentence+' '*2+'|')
print(' '*left_margin+'|'+' '*(box_width-2)+'|')
print(' '*left_margin+'+'+'-'*(box_width-2)+'+')