鱼C论坛

 找回密码
 立即注册
查看: 842|回复: 4

[技术交流] 大神们 我不知道怎么写前面两个条件

[复制链接]
发表于 2018-8-15 21:42:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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 + '.')
屏幕快照 2018-08-15 下午11.35.03.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-15 21:43:31 | 显示全部楼层
Program receives less than two arguments: print the message Not enough arguments.

Program receives more than two arguments: print the message Too many arguments.
就是这两个condition 不会 谢谢大佬们
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-15 21:52:56 | 显示全部楼层
  1. len(sys.argv)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-16 18:56:53 | 显示全部楼层

import sys
r = float(sys.argv[1])
h = float(sys.argv[2])
pi = 3.141592
value = pi * r**2 * h
if (len(sys.argv)) > 3:
        print('Too many arguments.')
elif (len(sys.argv)) < 3:
        print('Not enough arguments.')
elif 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 + '.')


但是 Traceback (most recent call last):
-   File "cylinder.py", line 3, in <module>
-     h = float(sys.argv[2])
- IndexError: list index out of range
唯一实现不了就是print(‘not enough arguments')

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-16 19:33:13 | 显示全部楼层
Flashneilyoung 发表于 2018-8-16 18:56
import sys
r = float(sys.argv[1])
h = float(sys.argv[2])

先len(sys.argv)即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 17:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表