猜年龄
num={'1','2','3','4','5','6','7','8','9','0'}
for i in range(10,40):
s=str(i**3)+str(i**4)
if len(set(s))==len(num):
print(i)
break
猜数字
count = 0
while True:
count=count+1
try:
a=input()
a=eval(a)
if(a==100):
break
elif(a>100):
print("larger than expected")
continue
elif(a<100):
print("less than expected")
except (ValueError,NameError,SyntaxError):
print("input error")
print('you have tried',count,'times, you win')
列表删除
import random
n = int(input())
ls = []
for i in range(1, n + 1):
ls.append(i)
print(ls)
random.seed(n)
m = random.randint(1, n)
i = 1
while True:
if m * i > n:
break
else:
if m * i in ls:
ls.remove(m * i)
i += 1
print(ls)
列表组合升序并且过滤掉重复数组元素
ls1 = list(map(int, input().split(' ')))
ls2 = list(map(int, input().split(' ')))
ls1.extend(ls2)
ls3 = []
for i in ls1:
if i not in ls3:
ls3.append(i)
ls3.sort()
for ch in ls3:
print(ch,end='')
绩点计算
sum1 = 0
r = 0
while True:
n = input()
if n == '-1':
break
elif n == 'A':
p = 4.0
elif n == 'A-':
p = 3.7
elif n == 'B+':
p = 3.3
elif n == 'B':
p = 3.0
elif n == 'B-':
p = 2.7
elif n == 'C+':
p = 2.3
elif n == 'C':
p = 2.0
elif n == 'C-':
p = 1.5
elif n == 'D':
p = 1.3
elif n == 'D-':
p = 1.0
elif n == 'F':
p = 0
m = eval(input())
r = r + m
sum1 = sum1 + m * p
print('%.2f' % (sum1 / r))
温度转换(异常处理)
t = input('What is the temperature?')
if t[-1] not in ['c', 'f', 'C', 'F']:
print("End with 'C','c','F','f'")
try:
if t[-1] in ['c', 'C']:
f = int(t[:-1]) * 1.8 + 32
print('The converted temperature is %dF' % f)
elif t[-1] in ['f', 'F']:
c = (int(t[:-1]) - 32) / 1.8
print('The converted temperature is %dC' % c)
except ValueError:
print('Input error')