1.用户输入两个数a、b。如果a能被b整除或a加b大于1000,则输出a;否则输出b。
代码:
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
if a % b == 0 or a + b > 1000:
print(a)
else:
print(b)
验证:
2.请输入一个数,判断这个数是偶数还是奇数,如果使用偶数,请判断从1到该数是3的倍数有哪些,如果是奇数,请判断从1到该数是5的倍数有哪些。
代码:
num = int(input("请输入一个数:"))
if num % 2 == 0:
for i in range(1,num + 1):
if i % 3 == 0:
print(i)
i += 1
else:
for i in range(1,num + 1):
if i % 5 == 0:
print(i)
i += 1
验证:
3.某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?
代码:
a = int(input("请输入购买的T恤数:"))
b = int(input("请输入购买的裤子数:"))
if a == 2:
money1 = 2 * 35 * 0.9
elif a >= 3:
money1 = a * 35 * 0.8
elif a == 1:
money1 = 35
else:
money1 = 0
if b >= 2:
money2 = b * 120 * 0.9
elif b == 1:
money2 = 120
else:
money2 = 0
print(f"你应该付{money1 + money2}元")
验证:
4.鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,兔有几只?
代码:
for chichen in range(1,36):
if chichen * 2 + (35 - chichen) * 4 == 94:
print(f"鸡有{chichen}只,兔有{35 - chichen}只")
chichen += 1
验证:
5.猜拳游戏:石头、剪刀、布的游戏
代码:
import random
while True:
coumpter = random.randint(0,3)
sign = int(input("请玩家输入你要出的:(0.石头 1.剪刀 2.布 )"))
print(coumpter)
if (coumpter == 0 and sign == 2) or (coumpter == 1 and sign == 0) or (coumpter == 2 and sign == 1):
print("玩家赢")
choice = input("是否继续(Y/N):")
if choice == "N" or choice == "n":
break
elif coumpter == sign:
print("平局")
choice = input("是否继续(Y/N):")
if choice == "N" or choice == "n":
break
else:
print("电脑赢")
choice = input("是否继续(Y/N):")
if choice == "N" or choice == "n":
break
验证: