|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目
Write a program that calculates the volume of a cylinder.
Your program will get the radius of the cylinder's base r and the height of the cylinder h as command line arguments. The radius will be given first.
Your program may encounter these unusual situations:
Program receives less than two arguments: print the message Not enough arguments.
Program receives more than two arguments: print the message Too many arguments.
Radius is negative: print the message Radius cannot be negative.
Height is negative (but radius is non-negative): print the message Height cannot be negative.
For all situations listed above, the program should terminate immediately after the message has been printed.
The value of pi should be set as a constant with the value 3.141592.
The volume of the cylinder should be printed to 2 decimal places, and can be calculated using the formula:
V=πr
2
h
Example 1:
$ python cylinder.py 4 10
The volume of the cylinder is 502.65.
---------------------------------
import sys
r = float(sys.argv[1])
h = float(sys.argv[2])
pi = 3.141592
value = pi * r**2 * h
if r <= 0:
print('Radius cannot be negative.' )
elif h <= 0:
print('Height cannot be negative.')
else:
print('The volume of the cylinder is ' + '%0.2f'%value + '.') |
-
|