1 永远的 HelloWorld
print("Hello World")
2M 与 N 的数学运算: 用户输入两个数 M 和 N, 其中 N 是整数, 计算 M 和 N 的 5 种数学运算结果, 并依次输出, 结果间用空格分隔
5 种数学运算分别是: M 与 N 的和 M 与 N 的乘积 M 的 N 次幂 M 除 N 的余数 M 和 N 中较大的值
- m = eval(input())
- n = eval(input())
- list = []
- list.append(str(m+n))
- list.append(str(m*n))
- list.append(str(m**n))
- list.append(str(m%n))
- if m> n:
- list.append(str(m))
- else:
- list.append(str(n))
- print(" ".join(tuple(list)))
3 将输入的字符串垂直输出
- def Output(string):
- for char in string:
- print(char)
- s = input()
- Output(s)
4 计算矩形面积: 用户输入矩形的长和宽, 计算其面积并输出, 结果四舍五入, 保留 2 位小数
- a = eval(input("请输入矩形长度:"))
- b = eval(input("请输入矩形宽度:"))
- print("面积是:%.2f"%(a*b))
5 计算 2 的 n 次方, n 由用户输入
- a = eval(input())
- print(2**a)
6 成绩转换: 编写一个学生成绩转换程序, 用户输入百分制的学生成绩, 成绩大于或等于 60 的输出 pass, 否则输出 fail, 成绩不四舍五入
- a = eval(input())
- if 100>= a>= 60:
- print("pass")
- else:
- print("fail")
7 完美立方: 找到大于 1 的 4 个整数满足完美立方等式: a3=b3+c3+d3(例如 123=63+83+103)编写一个程序, 对于任意给定的正整数 N(N 100), 寻找所有的四元组(a,b,c,d), 满足 a3=b3+c3+d3, 其中 1<a,b,c,dN
- n = int(input()) # n 范围内的立方数
- list_cube = [0] # 用于存储立方数的列表
- for i in range(1, n + 1):
- list_cube.append(i * i * i)
- for a in range(6, n + 1):
- for b in range(2, a - 1):
- if list_cube[a] <(list_cube[b] + list_cube[b + 1] + list_cube[b + 2]):
- break
- for c in range(b + 1, a):
- if list_cube[a] < (list_cube[b] + list_cube[c] + list_cube[c + 1]):
- break
- for d in range(c + 1, a):
- if list_cube[a] == (list_cube[b] + list_cube[c] + list_cube[d]):
- print("Cube=%d,Tripe=(%d,%d,%d)" % (a, b, c, d))
8 货币转换: 写一个程序进行货币间币值转换, 其中: 人民币和美元间汇率固定为: 1 美元 = 6.78 人民币
程序可以接受人民币或美元输入, 转换为美元或人民币输出人民币采用 RMB 表示, 美元 USD 表示, 符号和数值之间没有空格
- import re
- money = input().lower()
- tmp=re.findall('usd|rmb',money)
- if len(tmp)==0 or len(tmp)>1:
- print('wrong')
- money=re.sub(tmp[0],'',money)
- try:
- num=float(money)
- if 'usd' in tmp:
- print('RMB%.2f'%(num*6.78))
- else:
- print('USD%.2f'%(num/6.78))
- except:
- print('wrong')
9 月份缩写: 如果有 months = "Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec.", 编写一个程序, 用户输入一个月份的数字, 输出月份的缩写
- months = "Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec."
- n = input()
- #(每个月份的数字 - 1)*4 就是这个月份简写的开始索引, 截取 4 个字符
- index = (int(n)-1)*4
- month = months[index: index + 4]
- print(month)
10 温度转换: 编写程序将用户输入华氏度转换为摄氏度, 或将输入的摄氏度转换为华氏度
转换算法如下:(C 表示摄氏度 F 表示华氏度)
- C = ( F - 32 ) / 1.8
- F = C * 1.8 + 32
要求如下:
(1) 输入输出的摄氏度采用大写字母 C 开头, 温度可以是整数或小数, 如: C12.34 指摄氏度 12.34 度;
(2) 输入输出的华氏度采用大写字母 F 开头, 温度可以是整数或小数, 如: F87.65 指摄氏度 87.65 度;
(3) 不考虑异常输入的问题, 输出保留小数点后两位;
- Temperature = input()
- if Temperature[0] in ['F']:
- C = (eval(Temperature[1:])-32)/1.8
- print("C{:.2f}".format(C))
- else:
- F = 1.8*eval(Temperature[1:])+32
- print("F{:.2f}".format(F))
11 汇率兑换: 按照 1 美元 = 6 人民币的汇率来编写一个美元与人民币的双向兑换程序
- money = input()
- if money[-1] in ['$']:
- m = 6*eval(money[:-1])
- print("{:.2f}R".format(m))
- else:
- if money[-1] in ['R']:
- m = eval(money[:-1])/6
- print("{:.2f}$".format(m))
- else:
- print("输入错误!")
12 恺撒密码: 凯撒密码是古罗马凯撒大帝用来对军事情报进行加解密的算法, 它采用了替换方法对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符, 即, 字母表的对应关系如下:
- sr1 = "abcdefghijklmnopqrstuvwxyz"
- sr2 = sr1.upper()
- sr = sr1 + sr1 + sr2 + sr2
- in_str = input("")
- out_str = ""
- for j in in_str:
- if j == " ":
- out_str = out_str + " "
- continue
- i = sr.find(j)
- if(i> -1):
- out_str = out_str + sr[i+3]
- print(out_str)
- m = int(input())
- ans = 0
- if m> 3500:
- m1 = m - 3500
- if m1 < 1500:
- ans = 0.03 * m1
- elif m1 < 4500:
- ans = 0.1 * m1
- elif m1 < 9000:
- ans = 0.2 * m1
- elif m1 < 35000:
- ans = 0.25 * m1
- elif m1 < 55000:
- ans = 0.3 * m1
- elif m1 < 80000:
- ans = 0.35 * m1
- else:
- ans = 0.45 * m1
- else:
- ans = 0
- print("%.0f"%ans)
- import math
- list = []
- for i in range(100, 1000):
- x = math.floor(i / 100)
- y = math.floor((i - x * 100) / 10)
- z = i - math.floor(i / 10) * 10
- if i == x ** 3 + y ** 3 + z ** 3:
- list.append(str(i))
- print(",".join(tuple(list)))
- All that doth flow we cannot liquid name
- Or else would fire and water be the same;
- But that is liquid which is moist and wet
- Fire that property can never get.
- Then 'tis not cold that doth the fire put out
- But 'tis the wet that makes it die, no doubt.
- s = "All that doth flow we cannot liquid name Or else would fire and water be the same;But that is liquid which is moist and wet Fire that property can never get. Then'tis not cold that doth the fire put out But 'tis the wet that makes it die, no doubt."
- print("这首词总共有:" + str(len(s)) + "个字符串")
- print("这首诗是否以 All 开头:",s.startswith('All'))
- print("这首诗是否以 That\'s all, folks! 结尾:",s.endswith('That\'s all, folks!'))
- print("第一次出现单词 the 的位置:",s.find('the'))
- print("最后一次出现单词 the 的位置:",s.rfind('the'))
- print("the 在诗中出现的总次数:",s.count('the'))
- print("是否诗中出现的所有字符都是字母和数字:",s.isalnum())
来源: https://www.cnblogs.com/compassblog/p/8606255.html