Python学习(简单题)

目录

7-1 jmu-python-判断闰年

7-2 jmu-python-素数

7-3 jmu-python-找字符

7-4 计算表达式(*,//, %)

7-5 客户评级

7-6 运输打折问题

7-7 水仙花数

7-8 生成输入数的乘方表

7-9 输出字母在字符串中位置索引

7-10 通过两个列表构建字典

7-11 jmu-python-重复元素判定

7-12 求集合的最大值和最小值

7-13 字符替换

7-14 合并成绩

7-15 zust-sy7-11输出生肖和星座

7-16 录取排名


7-1 jmu-python-判断闰年

year = int(input())
if (year%4==0 and year%100!=0) or (year%400 ==0) :
    print(f'{year}是闰年')
else :
    print(f'{year}不是闰年')

7-2 jmu-python-素数

num=int(input())
if num<=1:
    print("{} is not prime".format(num))
else:
    for i in range(2,num):
        if num%i == 0:
            print("{} is not prime".format(num))
            break
    else:
        print("{} is prime".format(num))

7-3 jmu-python-找字符

n=input()
i=input()
a=n.find(i)+1
if a==0:
    print(f"can't find letter {i}")
else :
    print(f'index={a}')

7-4 计算表达式(*,//, %)

s = input()
s = s.replace(' ','')
try:
    print(s,eval(s),sep='=')
except:
    print('Invalid operator')

7-5 客户评级

xiaofei = int(input())
xiaofei = xiaofei/12
if xiaofei < 2000: 
    b = 1
elif xiaofei<3000:
    b = 2
elif xiaofei<4000:
    b = 3
elif xiaofei<5000:
    b = 4
else:
    b = 5
for i in range(0,b):    
    print("*",end="")   


7-6 运输打折问题

a,b=input().split(" ")
a,b=float(a),float(b)
if a>0:
    if 0<= b <250 :
        print(round(a*b*1.0))
    elif 250<= b <500:
        print(round(a*b*0.98))
    elif 500<= b <1000:
        print(round(a*b*0.95))
    elif 1000<= b <2000:
        print(round(a*b*0.92))
    elif 2000<= b < 3000:
        print(round(a*b*0.90))
    elif 3000<= b:
        print(round(a*b*0.85))
    else:
        print(0)
else:
    print(0)
        

7-7 水仙花数

n = int(input())
for num in range(10**(n-1),10**n):
    i = num
    sum = 0
    while (i > 0):
        a = i % 10
        sum += a ** n
        i = i // 10
    if sum == num:
        print(sum)

7-8 生成输入数的乘方表

a ,n = input().split()
n = int(n)
a = float(a)
for i in range(n + 1):
    print("{0:.1f}**{1:d}={2:.2f}".format( a,i, pow(a,i)))

7-9 输出字母在字符串中位置索引

str = input()
a, b = input().split()
i = len(str) - 1
str = str[::-1] 
for ch in str:
    if ch == a or ch == b:
        print(i, ch)
    i -= 1

7-10 通过两个列表构建字典

a=input().split(" ")
b=input().split(" ")
dic={}
for i in range(0,len(a)):
    dic[a[i]] = b[i]
a.sort()
result=[]
for i in range(0,len(a)):
    result.append((a[i],dic[a[i]],))
print(result)


7-11 jmu-python-重复元素判定

n=int(input())
true=false=0
for i in range(n):
    list=input()
    a=[]
    a=list.split()
    if len(a) == len(set(a)):
        false += 1
    else:
        true +=1
print("True={}, False={}".format(true,false))

7-12 求集合的最大值和最小值

dic=[-32,3,-55,234,21]
print('{',end = '')
print(', '.join(str(i)for i in dic),end='')
print('}')
print(max(dic))
print(min(dic))

7-13 字符替换

s = input()
result = ""
for i in s:
    if i.isupper():
        i = chr(ord('A') + ord('Z') - ord(i))
        result += i
    else:
        result += i
print(result)

7-14 合并成绩

a = tuple(map(int,input().split(',')))
b = tuple(map(int,input().split(',')))

alls = a+b
str1 = '  ' + '  '.join(f'{s:2}' for s in alls)
print(str1)

7-15 zust-sy7-11输出生肖和星座

def get_zodiac_animal(year):
    # 生肖循环周期为12年,以2000年为龙年开始计算
    animals = ['龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔']
    return animals[(year - 2000) % 12]

def get_constellation(month, day):
    # 星座依据月份和日期判断
    constellations = {
        (1, 20): '水瓶座', (2, 19): '双鱼座', (3, 21): '白羊座',
        (4, 20): '金牛座', (5, 21): '双子座', (6, 22): '巨蟹座',
        (7, 23): '狮子座', (8, 23): '处女座', (9, 23): '天秤座',
        (10, 23): '天蝎座', (11, 22): '射手座', (12, 22): '魔羯座'
    }
    for (m, d), name in constellations.items():
        if month > m or (month == m and day >= d):
            return name
    return '未知星座'

def main():
    birth_date = input()
    year, month, day = int(birth_date[:4]), int(birth_date[4:6]), int(birth_date[6:])
    
    zodiac_animal = get_zodiac_animal(year)
    constellation = get_constellation(month, day)
    
    print(f"您的生肖是:{zodiac_animal}")
    print(f"您的星座是:{constellation}")

if __name__ == "__main__":
    main()

7-16 录取排名

a={}
b=[]
while True:
    name=input()
    if name =='quit':
        break
    score = float(input())
    a[name]=score
items=a.items()
for j in items:
    b.append([j[1],j[0]])
b.sort()
count=len(b)-1
for i in range(1,len(b)+1):
    print(f"第{i}名:{b[count][1]},成绩为{b[count][0]}分")
    count = count-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值