【python基础语法实践应用】合肥工业大学python实验一题解

新手代码。如果有错误,请在评论区指正。持续更新
请使用ctrl + f查找题号

题目列表:

【python基础语法实践应用】合肥工业大学python实验一题解_第1张图片
【python基础语法实践应用】合肥工业大学python实验一题解_第2张图片
【python基础语法实践应用】合肥工业大学python实验一题解_第3张图片

第一题题解:

x = ((3**4 + (5 * (6**7))) / 8)**(0.5)
print(format(x, '.3f'))

第二题题解:

Is = ["综合", "理工", "综合", "综合", "综合", "综合", "综合", "综合", \
 "综合", "综合", "师范", "理工", "综合", "理工", "综合", "综合", \
 "综合", "综合", "综合", "理工", "理工", "理工", "理工", "师范", \
 "综合", "农林", "理工", "综合", "理工", "理工", "理工", "综合", \
 "理工", "综合", "综合", "理工", "农林", "民族", "军事"]

It = {}
for i in Is:
    if i in It:
        It[i] += 1
    else:
        It[i] = 1

for i in It:
    print(i,It[i])

第三题题解:

s = str(input("输入一串数字,以空格分开:"))
news = s.split(" ")
# 此时news为['1', '2', '3', '4']
news1 = []
for i in news:
    news1.append(int(i))
# 此时news1 = [1, 2, 3, 4]
num = 0
for i in news1:
    for j in news1:
        if i != j:
            for k in news1:
                if k != i and k != j:
                    print(100*i+10*j+k, " ", end="")
                    num += 1
print("共有个数:{}".format(num))

第四题题解:

s = input("输入一个字符串:")
for i in s:
    print(i)

第五题题解:

n = int(input("输入一个非负整数:"))
i = 0
while n > 0:
    if int(i) >= 0:
        print(int(i)**2)
    i += 1
    n -= 1

第六题题解:

n = int(input("输入一个正整数:"))
for i in range(n):
    print(i+1, end="")

第七题题解:

r = float(input("输入矩形的长或宽:"))
s = float(input("输入矩形未输入的边长:"))
print(r * s)

第八题题解:

r = float(input("输入矩形的长或宽:"))
s = float(input("输入矩形未输入的边长:"))
print(format(r*s, ".2f"))

第九题题解:

R = float(input("输入外径:"))
r = float(input("输入内径:"))
n = int(input("输入数量:"))
x = 3.14159

num = ((x*(R**2)) - (x*(r**2)))*n
print(format(num, '.2f'))
print(format(num, '.2f'))

第十题题解:

a = int(input("输入一个整数a:"))
b = int(input("输入一个整数b:"))

print("a整除b的结果是{}".format(a//b))
print("a除b的结果是{}".format(a/b))

第十一题题解:

print(sum(range(1, int(input("输入一个正整数")))))

第十二题题解:

n = int(input("输入一个正整数"))
temp = 0
sum = 0
for i in range(n):
    for j in range(int(i+1)):
        if j == i:
            temp *= 10
            temp += j+1
    sum += temp
print(sum)

第十三题题解:

n = int(input("输入整数n:"))
# 斐波那契数列
sum = f1 = f2 = 1
for i in range(1, n):
    f = f1 + f2
    f1, f2 = f2, f
    if i % 2 == 0:
        sum += i/f
    else:
        sum -= i/f
print(format(sum, '.6f'))

第十四题题解:

n = eval(input("输入一个正整数:"))
sum = 0
for i in range(n):
    sum += (i+1)*(i+1)
print(sum)

第十五题题解:

n = eval(input("输入整数n:"))
for i in range(n):
    print("{} ".format(i), end="")

第十六题题解:

n = eval(input("输入n:"))
print(2**n)

第十七题题解:

n = int(input("输入整数n:"))
# 斐波那契数列
sum = 0
f1, f2 = 1, 2
for i in range(n):
    sum += f2/f1
    f = f1 + f2
    f1, f2 = f2, f
print(format(sum, '.6f'))

第十八题题解:

n = int(input("输入整数n:"))
sum = 0
for i in range(n):
    temp = 1
    for j in range(i+1):
        temp *= j+1
    sum += temp
print(sum)

第十九题题解:

n = int(input("输入整数n:"))
sum = 0
for i in range(n):
    temp = 1
    for j in range(i+1):
        temp = 2*(j+1) - 1
    sum += temp
print(sum)

第二十题题解:

fp = open("D:\Pyscore.txt", 'r') #txt文件放到对应位置即可,最好不要放在c盘
str = fp.read()
newstr = str.split(' ')
sum = 0
num = 0
n = len(newstr)
for i in newstr:
    sum += float(i)
    if float(i) >= 60:
        num += 1
print(int(sum//n))
print(num)

第二十一题题解:

n = int(input("输入一个正整数n:"))

if (n % 2) == 0:
    print("even")
else:
    print("odd")

第二十二题题解:

a = eval(input("输入a:"))
b = eval(input("输入b:"))
print(format(a/b, '.2f'))

#print(round(1.345,2)) # 四舍六入五成双

第二十三题题解:

year = int(input("输入一个正整数表示年份:"))
flag = 0 #初始表示不是闰年
if (year % 100) != 0:
    if (year % 4) == 0:
        flag = 1
else:
    if (year % 400) == 0:
        flag = 1

if flag == 1:
    print("该年有366天")
else:
    print("该年有365天")

第二十四题题解:

# 海伦公式求三角形面积
a = eval(input("输入a:"))
b = eval(input("输入b:"))
c = eval(input("输入c:"))

s = [a, b, c]
s.sort()
p = (a + b + c) / 2
if s[0] + s[1] > s[2]:
    area = float((p * (p-a) * (p-b) * (p-c))**0.5) # 此处使用eval()会报错
    print("YES",format(area, '.2f'))
else:
    print("NO")

第二十五题题解:

a = eval(input("输入a:"))
b = eval(input("输入b:"))
c = eval(input("输入c:"))

s = [a, b, c]
s.sort()
if s[0]**2 + s[1]**2 == s[2]**2:
    print("YES")
else:
    print("NO")

第二十六题题解:

sex = str((input("输入性别:")))
father = int(input("父亲身高:"))
mother = int(input("母亲身高:"))
if sex == "男":
    child = (father + mother)*1.08/2
    print(int(child))
elif sex == "女":
    child = (father * 0.923 + mother)/2
    print(int(child))
else:
    print("无对应公式")

第二十七题题解:

name = str(input("输入用户名:"))
mima = int(input("输入密码:"))

if (name == "admin") & (mima == 123456):
    print("登陆成功")
else:
    print("登录失败")

第二十八题题解:

n = int(input("输入一个正整数"))
temp = 0
sum = 0
if n < 10:
    for i in range(n):
        for j in range(int(i+1)):
            if j == i:
                temp *= 10
                temp += j+1
        sum += temp
    print(sum)
else:
    print("data error")

第二十九题题解:

str = str(input("输入IP地址:"))
newstr = str.split('.')
flag = 1
for i in newstr:
    if (int(i) < 0) | (int(i) > 255):
        # 这里使用i而不要使用newstr[i]...
        flag = 0
if len(newstr) != 4:
    flag = 0

if flag == 1:
    print("YES")
else:
    print("NO")

第三十题题解:

s = str(input("输入一个完全由数字字符组成的字符串 s:"))
oddsum = 0
evensum = 0
for i in s:
    if int(i) % 2 == 0:
        evensum += int(i)
    else:
        oddsum += int(i)

print("oddsum=", oddsum, "  evensum=", evensum)

第三十一题题解:

str = str(input("输入一个英文句子:"))
newstr = str.split(" ")
print(len(newstr))

第三十二题题解:

s = str(input("输入显示的验证码:"))
s = s.upper()
# 将str小写字母转大写字母
if s == "QS2X":
    print("验证码正确")
else:
    print("验证码错误,请重新输入")

第三十三题题解:

s = str(input("输入以空格分隔的多个商品名:"))
It = {}
news = s.split(" ")
for i in range(len(news)):
    It[i] = news[i]
for i in It:
    print(i, It[i])

第三十四题题解:

dict1 = {'赵广辉':'13299887777','特朗普':'814666888','普京 \
':'522888666','吴京':'13999887777'}
name = str(input("输入姓名:"))
if name in dict1:
    print(name, dict1.get(name))
else:
    print("数据不存在")

第三十五题题解:

dict1 = {'admin':'123456', 'administrator':'12345678', 'root':'password'}
flag = 0
# 用来记录输入错误次数

while flag < 3:
    name = str(input("输入用户名:"))
    mima = str(input("输入密码:"))
    if (name in dict1) | (mima == dict1.get(name)):
        print("登陆成功")
        break
    else:
        print("登陆失败,请重新输入:")
        flag += 1

第三十六题题解:

s = str(input("输入不含数字的字符串:"))
flag = 1
while 1 > 0:
    flag = 1 # 此行更新flag代码必须要有
    for i in s:
        if i.isnumeric():
            print("请重新输入:")
            s = str(input())
            flag = 0
            break
    if flag == 1:
        break
print(len(s))

你可能感兴趣的:(python实验,python)