实验3

2021/4/20 18:27:02

本文主要是介绍实验3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


x = list(range(10))
print('整数输出1:',end='')
for i in x:
    print(i,end=' ')

print('\n整数输出2:',end='')
for i in x:
    print(f'{i:02d}',end='-')

print('\n整数输出3:',end='')
for i in x[:-1]:
    print(f'{i:02d}',end='-')
print(f'{x[-1]:02d}')

print('\n字符输出1:',end='')
y1=[str(i) for i in range(10)]
print('-'.join(y1))

print('字符输出2:',end='')
y2=[str(i).zfill(2) for i in range(10)]
print('-'.join(y2))

print('整数列表:')
x=list(range(97,97+26))
for i in x:
    print(i,end=' ')

print('\n字符列表1:')
lower_alpha = [chr(i) for i in range(97,97+26)]
for i in lower_alpha:
    print(i,end=' ')

print('\n字符列表2:')
upper_alpha = [i.upper( )for i in lower_alpha]
for i in upper_alpha:
print(i,end=' ')

import random
import string

print('用列表存储随机整数:')
ls=[random.randint(1,100) for i in range(5)]
print(ls)

print('\n用集合存储随机整数:')
s={random.randint(1,100) for i in range(5)}
print(s)

print('\n\n字母串:',end=' ')
alpha=string.ascii_letters
print(alpha)
print(type(alpha))

print('\n数字串:',end=' ')
digits=string.digits
print(digits)
print(type(digits))

x=[0,1]
a=0
b=1
c=1
d=0
while b <=1000:
    a=b
    b=b+x[c]
    c+=1
    x.append(a)
for m in x:
    print("{:>3d}".format(m),end=" ")
    d+=1
    if d%5==0:
        print(" ")

import random
i=2
print('猜猜2021年5月哪一天是你的lucky day 你有3次机会 猜吧')
x=int(input('请输入你想要猜的天数(1-31):'))
y=random.randint(1,31)
while x!=y:
    if i <= 3:
        if x<y:
            print('猜早啦')
            i+=1
            x=int(input('再猜,请输入你想要猜的天数(1-31):'))
        elif x>y:
            print('猜晚啦')
            i+=1
            x=int(input('再猜,请输入你想要猜的天数(1-31):'))
    else:
        print('您的机会用完啦')
        break
else:
    print('恭喜你,猜对啦')

num = int(input("输入十进制数字:",))
n = int(input("输入要转换的进制:",))
if n == 2:
    print("转换为二进制为:", bin(num))
if n == 8:
    print("转换为八进制为:", oct(num))
if n == 16:
    print("转换为十六进制为:", hex(num))



这篇关于实验3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程