目录
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 triangleEnter 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