Python:流程控制

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:
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值