1.用户输入两个数a、b。如果a能被b整除或a加b大于1000,则输出a;否则输出b。
a = float(input("输入一个数")) b = float(input("第二个数")) if a % b == 0 or a + b > 1000: print(a) else: print(b)
2.请输入一个数,判断这个数是偶数还是奇数,如果使用偶数,请判断从1到该数是3的倍数有哪些,如果是奇数,请判断从1到该数是5的倍数有哪些
a = int(input("请输入一个数")) if a % 2 == 0: print(f"{a}为偶数,从1到{a}是3倍数有:") for x in range(1,a+1): if x % 3 == 0: print(x) else: print(f"{a}为奇数,从1到{a}是5倍数有:") for x in range(1,a+1): if x % 5 == 0: print(x)
3.某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?
a = 35 b = 120 c = int(input("请输入买几件T恤:")) d = int(input("请输入买几件裤子:")) if c == 2: a = a * 0.9 if c >= 3: a = a * 0.8 if d >= 2: b = b * 0.9 print(f"小明应该付{c * a + d * b}元")
4.鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,兔有几只?
for c in range(0,36): r = 35 - c if c * 2 + r * 4 == 94: print(f"鸡有{c}只,兔子有{r}只")
import random a = random.choice(["石头","剪刀","布"]) b = str(input("输入你的")) if b == "石头" or b == "剪刀" or b == "布": if a == "石头": print(f"机器输的是{a}") if b == "石头": print("平手") elif b == "剪刀": print("输了") elif b == "布": print("平手") if a == "剪刀": print(f"机器输的是{a}") if b == "石头": print("赢了") elif b == "剪刀": print("平手") elif b == "布": print("输了") if a == "布": print(f"机器输的是{a}") if b == "石头": print("输了") elif b == "剪刀": print("赢了") elif b == "布": print("平手")
6.判断以下哪些不能作为标识符(BCDF)
A、a
B、¥a
C、_12
D、$a@12
E、false
F、False
7.求50~150之间的质数是那些?
for i in range(50,150):
flag = True
for j in range(2,i):
if i % j == 0:
flag = False
break
if flag:
print("%s是质数" %(i))
8.打印输出标准水仙花数,输出这些水仙花数
for x in range(100,1000):
s = str(x)
a = int(s[0])
b = int(s[1])
c = int(s[2])
if x == a**3 + b**3 + c**3:
print(x)
9.验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除.
def iszhengchu9(x):
'''
验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除
'''
sum_x = 0
ls = list(str(x))
for i in ls:
num = int(i)
sum_x += num
if x > 9 and (x - sum_x)%9 == 0:
return True
else:
return False
x = int(input('请输入任意大于9的整数以验证:'))
if iszhengchu9(x):
print(f'{x}验证成功')
else:
print(f'{x}验证失败')
14.将list中的重复数据去重,至少使用两种方案
li=[1,2,3,4,5,1,2,3]
new_li=[]
for i in li:
if i not in new_li:
new_li.append(i)
print(new_li)
15.两个列表进行合并操作
>>>c1 = ["Red","Green","Blue"]
>>>c2 = ["Orange","Yellow","Indigo"]
>>>c1.extend(c2)
>>>c1 #["Red","Green","Blue","Orange","Yellow","Indigo"]
16.使用列表判断一个列表是否在另外一个列表中
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3在列表中")
else:
print("3不在列表中")
17.列表的反转 列表的排序
list = [2, 1, 3, 5, 4]
list.reverse()
print(list)
#[4, 5, 3, 1, 2]
list = [2, 1, 'hello', 5, 'world']
list.reverse()
print(list)
#['world', 5, 'hello', 1, 2]
以上即是本次所有内容,希望对您有所帮助!!