Python两个if语句 [英] Python two if statements
问题描述
所以即时尝试编写一个函数,其中给定角度必须小于90度,大于0.并且对于raidan,小于pi / 2且大于0.
这是我的功能
def is_valid_angle(s:str) - > bool:
当且仅当s是有效角度时才返回True。请参阅作业
描述和示例以获取更多信息关于什么是有效的
例如:
>>> is_valid_angle(85.3d)
真实
>>> is_valid_angle(85.3.7D)
False
>>> is_valid_angle(90d )
False
>>> is_valid_angle(0.001r)
True
> ;>> is_valid_angle(1.5R)
True
如果s [-1] =='r ''R':
如果s< (pi / 2):
如果s> 0:
返回true
否则:
如果s [-1] =='d'D':
如果s< 90:
如果s> 0:
返回true
我不明白什么是错的
我尝试过:
def is_valid_angle(s:str) - > bool:
当且仅当s是有效角度时才返回True。请参阅作业
描述和示例以获取更多信息关于什么是有效的
例如:
>>> is_valid_angle(85.3d)
真实
>>> is_valid_angle(85.3.7D)
False
>>> is_valid_angle(90d )
False
>>> is_valid_angle(0.001r)
True
> ;>> is_valid_angle(1.5R)
True
如果s [-1] =='r ''R':
如果s< (pi / 2):
如果s> 0:
返回true
否则:
如果s [-1] =='d'D':
如果s< 90:
如果s> 0:
返回true
so im trying to code a function where, the given angle must be less than 90 for degree and greater than 0. and also for a raidan, less than pi/2 and greater than 0.
this is my function
def is_valid_angle(s:str)-> bool:
"""
Returns True if and only if s is a valid angle. See the assignment
description and examples for more information regarding what's valid
Examples:
>>> is_valid_angle("85.3d")
True
>>> is_valid_angle("85.3.7D")
False
>>> is_valid_angle("90d")
False
>>> is_valid_angle("0.001r")
True
>>> is_valid_angle("1.5R")
True
"""
if s[-1]=='r''R':
if s < (pi/2):
if s > 0:
return true
else:
if s[-1]=='d''D':
if s < 90:
if s > 0:
return true
I dont understand whats wrong
What I have tried:
def is_valid_angle(s:str)-> bool:
"""
Returns True if and only if s is a valid angle. See the assignment
description and examples for more information regarding what's valid
Examples:
>>> is_valid_angle("85.3d")
True
>>> is_valid_angle("85.3.7D")
False
>>> is_valid_angle("90d")
False
>>> is_valid_angle("0.001r")
True
>>> is_valid_angle("1.5R")
True
"""
if s[-1]=='r''R':
if s < (pi/2):
if s > 0:
return true
else:
if s[-1]=='d''D':
if s < 90:
if s > 0:
return true
推荐答案
尝试类似
Try something like
import math
def is_valid_angle(s:str)-> bool:
v = float(s[:-1])
if s[-1]=='r' or s[-1] == 'R':
if v < (math.pi/2):
if v > 0:
return True
else:
if s[-1]=='d' or s[-1]=='D':
if v < 90:
if v > 0:
return True
return False
这篇关于Python两个if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!