1.直角坐标系象限判断:
while True:
try:
list1 = [int(i) for i in input().split()]
if(list1[0]>0 and list1[1]>0):
print("1")
elif(list1[0]>0 and list1[1]<0):
print('4')
elif(list1[0]<0 and list1[1]>0):
print('2')
else:
print('3')
except:
break
实现多组的输入与输出可以用try-except方法实现
2.计算分段函数:
num = float(input())
def g(num):
if num==0:
print("g(%.3f) = %.3f"%(0,0))
else:
num2 = 1/(2*num)
print('g(%.3f) = %.3f'%(num,num2))
g(num)
这里要注意的是格式化输出两个数时的写法,以及转换为浮点类型
3.汽车超速罚款
list1 = [int(i) for i in input().split()]
if(list1[0]>list1[1]):
print('Congratulations, you are within the speed limit!')
else:
if(list1[1]-list1[0] <= 20):
print('You are speeding and your fine is 100.')
elif(20< (list1[1]-list1[0]) <=30):
print('You are speeding and your fine is 270.')
else:
print('You are speeding and your fine is 500.')
没有需要注意的点
4.运输计费问题:
//list1 = [float(i) for i in input().split()]
//a = list1[0]
//b = list1[1]
a, b = map(float,input().split())
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: