前言:
以下这些小练习,很少用到函数和类去编写的哦。在后期的第三篇中,我会重点地用函数和类去编写一些小小的练习。感谢各位友友们的支持,你们支持就是我最大的动力,欢迎各位友友们提出你们宝贵的意见,我会一一回复的呐~
# if函数的简单应用1
x=int(input("please input a int num:\n"))
if x%2==0 and x%3==0:
print("Yes")
else:
print("No")
# if 函数简单应用2
Input=float(input("please input your PM2.5:\n"))
if Input>=75:
print("当日空气质量:污染")
elif Input>=35:
print("当日空气质量:良")
else:
print("当日空气质量:优")
# if函数简单应用3
x=int(input("please input your x:\n"))
if x>=10:
y=(x-5)**2
print(y)
else:
if 5<=x<10:
y=5*x-25
print(y)
else:
y=0
print(y)
# 编程任务:输出完美数
for m in range(1,10000):
sum,num=0,0
for n in range(1,m):
if m%n==0:
sum+=n
num+=1
if sum==m:
print(f'1到10000内第 {num} 位完美数是:{sum}\n')
#编程任务:输出由1、2、3、4四个数字组成的各位不相同的三位数
count = 1
for m in range(1, 5):
for n in range(1, 5):
if m!=n:
for p in range(1, 5):
if p!=m and p!=n:
if count==15:
break
else:
print("第 {} 次输出: {} {} {}".format(count,m,n,p))
print()
count+=1
print()
print(f'一共输出{count}个由1、2、3、4四个数字组成的各位不相同的三位数')
编程任务:几位评委打分,去掉最高和最低分,输出最终平均打分
def AVG(a,b):
score=[]
for m in range(x,y+1):
num=float(input("Please enter the {} of judges'score:\n".format(m)))
score.append(round(num,2)) #只保留两位小数
score_ASE=sorted(score)
print("去掉最高和最低分之前:")
print(score_ASE)
del score_ASE[0]
score_ASE.pop()
print("去掉最高和最低分之后:")
print(score_ASE)
sum=0
for n in range(len(score_ASE)):
sum+=score_ASE[n]
avg=sum/len(score_ASE)
print("最终平均分:",avg)
x=1
y=int(input("请输入有几位评委:"))
AVG(x,y)
# 编程任务:商场购物优惠价,现打八折
money=float(input("How much to buy these:")) if money>=0 and money<=10: money_discouts=money-(money*0.1) print("discouts_10%","you need pay ",money_discouts ,"for these") else: money_discouts=money-(money*0.2) print("discouts_20%","you need pay ",money_discouts,"for these")
# 编程任务: 猜数字游戏
# 设计功能: #实现猜字游戏 #随机产生一个数,把猜数和次数初置为0 #输入一个数,判断猜数和随机产生数大小 #大于显示太大,小于显示太小,等于则结束,一轮循环完则次数加一 #机会给10次 import random secret=random.randint(1,100) guess=0 degree=0 print("The guessing game begins Now!\n") while guess!=secret and degree<10: guess=int(input("input your number:\n")) if guess>secret: print("your number is too large") elif guess<secret: print("your number is too small") degree+=1 if guess==secret: print("You win You got it!") else: print("No chance,Better Luck net time goodbye!!!")
# 编程任务:设置入足球队的条件
print("Please keybord male or female,understand???") sex=input("what's your sex:") if sex=="female": age=int(input("what's your age ?")) if age>=10 and age<=12: print("Welecome to our score team!") else: print("you age not fit") else: print("No,In case of male")
# 编程任务:设置网游年龄及年级限制
age=float(input("your age Now:")) grade=int(input("your grade Now:")) if age>=8 and grade>=3: print("you got it,starting this game Now !") else: print("Sorry,you can't play the game !")
#编程任务:预估汽车加油
tank=float(input("Please key tank ?")) full=float(input("Please key full ?")) per=float(input("please key per ?")) distance=tank*per*full/100 if distance>=0 and distance<=200 and full >=0 and full<=1: Now=1-full disttance=tank*per*full/100 print("You go another",distance,"km") print("加满油还需要",Now*tank,"升") else: print("You can go another",distance) print("The next gas station is 200 km away") print("You can wait for the next station")
# format 格式输出
print('{}{}'.format('Hello ','world')) print("……………………") print('{0}{1}'.format('Hello','world')) print("……………………") print('{0}{1}{0}'.format('Hello',',','world')) print("……………………") print("{1}{1}{0}".format("Hello","world")) print("……………………") print('{a}{b}{a}'.format(b='Hello',a='world')) print("……………………")
#编程任务:判断奇偶年
print("判断奇偶年") year=input("请输入年份:") rem4=int(year)%4 rem100=int(year)%100 rem400=int(year)%400 if rem4==0: if rem100!=0 or rem400==0: print("%s是润年"%year) else: print("%s不是润年"%year) else: print("%s不是润年"%year)
# f格式输出
name="婷婷" age=17 print(f'姓名:{name},年龄:{age}') print("……………………………………")
# 简单写入文件
file=open(r'D:\Test\write.txt','w') file.write("我在天津很想你呐") file.close()