循环分支结构练习
程序员文章站
2022-03-02 12:09:24
...
1.系统登录界面
import getpass
username = input("请输入用户名:")
password = getpass.getpass("请输入密码:")
if username == "admin" and password == "123456":
print("欢迎使用本系统")
else:
print("用户名或密码错误,登录失败!")
2.y = 5 * x + 3 (x< -1)
y = 2 * x - 1 (-1<x<1)
y = 3 * x - 5 (x>1)
x = float(input("请输入x的值:"))
if x < -1:
y = 5 * x + 3
elif x < 1:
y = 2 * x - 1
else:
y = 3 * x - 5
print("y=",y)
3.输入成绩得到对应的等级grade = int(input("请输入你的成绩:"))
if 0<=grade < 60:
print("不及格")
elif 60<=grade < 70:
print("中")
elif 70<=grade < 85:
print("良")
elif 85<=grade <=100:
print("优")
else:
print("成绩输入有误")
4.得到随机数,通过分支语句打印出来from random import random,randint
x = randint(1, 6)
if x == 1:
print(1)
elif x == 2:
print(2)
elif x == 3:
print(3)
elif x == 4:
print(4)
elif x == 5:
print(5)
elif x == 6:
print(6)
5. 执行60000次1-6的随机整数,得到每一面出现的次数from random import randint
f1 = 0
f2 = 0
f3 = 0
f4 = 0
f5 = 0
f6 = 0
num = 0
while num<= 60000:
face = randint(1, 6)
if face == 1:
f1 += 1
elif face == 2:
f2 += 1
elif face == 3:
f3 += 1
elif face == 4:
f4 += 1
elif face == 5:
f5 += 1
else:
f6 += 1
num += 1
print("1点出现了%d次" % f1)
print("2点出现了%d次" % f2)
print("3点出现了%d次" % f3)
print("4点出现了%d次" % f4)
print("5点出现了%d次" % f5)
print("6点出现了%d次" % f6)
6.使用for... in 循环'''
print("hello,world! \n" * 10)
'''
import time
for _ in range(30):
print("hello, world!", sep=":")
time.sleep(1)
7.得到100以内所有偶数或奇数的总和my_sum = 0
for num in range(0, 101, 2):
my_sum += num
print(my_sum)
my_sum1 = 0
for num in range(1, 101, 2):
my_sum1 += num
print(my_sum1)
my_sum2 = 0
for num in range(101):
if num % 2 == 0 :
my_sum2 +=num
print(my_sum2)
my_sum3 = 0
for num in range(101):
if num % 3 == 0 or num % 5 ==0 :
my_sum3 += num
print(my_sum3)
8.计算出100以内能被3或5整除的整数的和totalnum = 0
num = 0
while num <101:
if num % 3 == 0 or num % 5 ==0:
totalnum += num
num += 1
print(totalnum)
9.人机猜数字-机器猜from random import randint
num_x = randint(0, 100)
try_time = 0
while 1:
num = int(input("请输入你要猜的数字是:"))
if num_x == num:
print("恭喜你答对了")
break
elif num_x < num:
print("数字猜大了")
try_time +=1
if try_time == 7 :
print("7次都猜错了,智商着急啊你,不给你猜了")
break
elif num_x > num:
print("数字猜小了")
try_time +=1
if try_time == 7 :
print("7次都猜错了,智商着急啊你,不给你猜了")
break
10.人机猜数-人猜#2.反转的猜数字(人出数字计算机猜)
from random import randint
from time import sleep
correct_num = 61
is_correct = False
while not is_correct:
machine_num = randint(0,100)
print(machine_num)
if machine_num == correct_num:
print("你猜对了")
is_correct = True
elif machine_num < correct_num:
print("数字小了")
else:
print("数字大了")
sleep(1)
11.人机猜拳import random
machine_property = 1000
my_property = 1000
while 1:
#1 是剪刀 剪刀赢布 输石头 平剪刀
#2 是布 布赢拳 输剪刀 平布
#3 是拳 拳赢剪刀 输布 平拳
#定制规则如下
my_finger = int(input("请输入你的选择:"))
machine_finger = random.randint(1, 3)
if my_property < 0 or machine_property <0:
print("游戏结束")
break
elif my_finger == 1 and machine_finger ==2 :
my_property += 100
machine_property -=100
print("电脑输了:我%d元,电脑%d元" % (my_property,machine_property))
elif my_finger == 1 and machine_finger ==3:
my_property -= 100
machine_property += 100
print("电脑赢了:我%d元,电脑%d元" % (my_property, machine_property))
elif my_finger == 2 and machine_finger ==1 :
my_property -= 100
machine_property +=100
print("电脑输了:我%d元,电脑%d元" % (my_property,machine_property))
elif my_finger == 2 and machine_finger ==3:
my_property += 100
machine_property -= 100
print("电脑赢了:我%d元,电脑%d元" % (my_property, machine_property))
elif my_finger == 3 and machine_finger ==2 :
my_property -= 100
machine_property +=100
print("电脑输了:我%d元,电脑%d元" % (my_property,machine_property))
elif my_finger == 3 and machine_finger ==1:
my_property += 100
machine_property -= 100
print("电脑赢了:我%d元,电脑%d元" % (my_property, machine_property))
else:
print("平局:我%d元,电脑%d元" % (my_property, machine_property))
12.计算个人所得税salary = float(input("本月收入:"))
insurance = float(input("五险一金:"))
diff = salary - insurance - 3500
if diff <=0:
tax = 0
deduction = 0
elif diff <= 1500:
tax = 0.03
deduction = 0
elif diff <=4500:
tax = 0.1
deduction = 105
elif diff <=9000:
tax = 0.2
deduction = 555
elif diff <= 35000:
tax = 0.25
deduction = 1005
elif diff <= 55000:
tax = 0.3
deduction = 2755
elif diff <= 80000:
tax = 0.35
deduction = 5505
else:
tax = 0.45
deduction = 13505
上一篇: textarea 限制输入长度
下一篇: 分支与循环练习