我需要一些指导,告诉我为什么在运行程序后没有在shell中打印任何内容.这是我的代码:
def numbersize1(x):
"""returns the string 'Negative!' if the number is less than zero, 'Small!' if the number is at least zero but less than 10, 'Medium!' if the number is at least 10 but less than 100, and 'Large!' if the number is greater than or equal to 100.
Number -> str"""
if x<0:
return str('Negative!')
if x==0 and x<10:
return str('Small!')
if x==10 and x<100:
return str('Medium!')
if x==100 and x>100:
return str('Large!')
Run Code Online (Sandbox Code Playgroud)
正如我之前所说,当我运行脚本时,shell中没有任何内容.换句话说,如果我输入'1',字符串'Small!' 应该出现,而不是.任何帮助表示赞赏.
如果你这样写:
if x==0 and x<10:
Run Code Online (Sandbox Code Playgroud)
你说的是x等于零,它小于10.如果x不等于零,if则不匹配.
与你的其他人一样.看看他们说什么,想想他们将要做什么.
您不必将字符串转换为字符串,因此您可以将其简化为:
if x<0:
return 'Negative!'
if x<10:
return 'Small!'
if x<100:
return 'Medium!'
return 'Large!'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |