Python 例题(7)

目录

Python 例题(7) 

1金融问题货币兑换:

 源代码:

 测试结果: 

2.几何问题,点在三角形内吗?

源代码:

测试结果:

3.找出可被5或六整除但不能被它两同时整除的数字

源代码:

测试结果:

4.在金字塔模式中显示数字:

源代码:

测试结果:


Python 例题(7) 

1金融问题货币兑换:

 源代码:

d = float(input("Enter the exchange rate from dollars to RMB:"))
versa = float(input("Enter 0 to convert dollars to RMB and 1 vice versa:"))
if versa == 0:
    dollar = int(input("Enter the dollar amount: "))
    print('$', dollar, "is", dollar * d, 'yuan')
elif versa == 1:
    RMB = float(input("Enter the RMB amount:"))
    print(RMB, "yuan is $%.2f" % (RMB / d))
else:
    print('Incorrect input')

 测试结果: 

Enter the exchange rate from dollars to RMB:6.81
Enter 0 to convert dollars to RMB and 1 vice versa:0
Enter the dollar amount: 100
$ 100 is 681.0 yuan


Enter the exchange rate from dollars to RMB:6.81
Enter 0 to convert dollars to RMB and 1 vice versa:1
Enter the RMB amount:10000
10000.0 yuan is $1468.43


Enter the exchange rate from dollars to RMB:6.81
Enter 0 to convert dollars to RMB and 1 vice versa:5
Incorrect input

2.几何问题,点在三角形内吗?

源代码:

(x, y) = eval(input("Enter a point's x- and y- coordinates:"))
if x > 0 and y > 0 and x + 2 * y - 200 < 0:
    print("The point is in the triangle")
else:
    print("The point is not in the triangle")

测试结果:

Enter a point's x- and y- coordinates:100.5,25.5
The point is in the triangle

Enter a point's x- and y- coordinates:100.5,50.5
The point is not in the triangle

3.找出可被5或六整除但不能被它两同时整除的数字

源代码:

print('输出100-200之间可以被5或6整除但不能被它俩同时整除的数:')
j = 0
for i in range(100, 201):
    if i % 5 == 0 or i % 6 == 0 and i % 30 != 0:
        print(i, end=' ')
        j = j + 1
        if j == 10:
            print('')
            j = 0

测试结果:

输出100-200之间可以被5或6整除但不能被它俩同时整除的数:
100 102 105 108 110 114 115 120 125 126 
130 132 135 138 140 144 145 150 155 156 
160 162 165 168 170 174 175 180 185 186 
190 192 195 198 200 
 

4.在金字塔模式中显示数字:

源代码:

h = int(input('请输入图形高度:'))
for i in range(h):
    for j in range(h - i - 1):
        print(" ", end='\t')
    for k in range(i + 1):
        print(2 ** k, end='\t')
        k += 1
        t = i
    for k in range(i):
        print(2 ** (t - 1), end='\t')
        t = t - 1
        k += 1
    print()

测试结果:

请输入图形高度:5
                    1    
               1    2    1    
          1    2    4    2    1    
     1    2    4    8    4    2    1    
1    2    4    8    16    8    4    2    1    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

White乄joker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值