Python-如何给数字一个描述



你们能帮我确定如何做到这一点吗:

编写一个函数,确定取其值的某个整数n的某些特性根据用户输入。这些特征包括:•如果n是奇数或偶数•如果n为正、负或两者都不为•如果n可被3整除•如果n的绝对值介于50和99之间(包括50和99(在确定其特性后,您的输出应该是这样的:

输入数字:69

===69的简短描述===

69是奇数

69为阳性

69可被3 整除

69的绝对值介于50和99之间(包括50和99(。


这是我的代码:

n = int(input("ENTER A NUMBER: "))
remainder = n % 2
print("===","SHORT DESCRIPTION OF", n,"===")
if (remainder == 0):
print(n, "is an EVEN NUMBER")
elif (n % 2) == 0:
print(n, "is an EVEN NUMBER")
else:
print(n, "is an ODD NUMBER")
if n > 0:
print(n, "is a POSITIVE NUMBER")
elif n < 0:
print(n, "is a NEGATIVE NUMBER")
else:
print(n, "is NEITHER a POSTIVE or a NEGATIVE NUMBER")
if (n % 3) == 0:
print(n, "is DIVISIBLE by 3")
else:
print(n, "is not DIVISIBLE by 3")
if 99 >= n >= 50:
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif n < 0:
99 >= n >= 50
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif 99 <= n >= 50:
print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

这几乎是正确的,但我面临的问题是代码的最后一部分,即如何使用";‘n’的绝对值在50和99之间(包括50和99(;。

如果n<0,则可以测试n或-n是否在50和99之间,在所有其他情况下,绝对值不在这两者之间。

这里有一个可能的解决方案:

if 99 >= n >= 50 or 99 >= -n >= 50:
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
else:
print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

当实际上没有必要的时候,你也可以重新测试相反的条件。如果第一个条件为假,则意味着它不在50到99之间,无需再次测试。

作为一个额外的建议,这里有一些对代码其余部分的轻微改进:

n = int(input("ENTER A NUMBER: "))
print("===","SHORT DESCRIPTION OF", n,"===")
#You were testing this condition twice!
if (n % 2) == 0:
print(n, "is an EVEN NUMBER")
else:
print(n, "is an ODD NUMBER")
if n > 0:
print(n, "is a POSITIVE NUMBER")
elif n < 0:
print(n, "is a NEGATIVE NUMBER")
else:
print(n, "is NEITHER a POSTIVE or a NEGATIVE NUMBER")
if (n % 3) == 0:
print(n, "is DIVISIBLE by 3")
else:
print(n, "is not DIVISIBLE by 3")
# you can test both condition at once so you don't have to copypaste the print statement
if 99 >= n >= 50 or 99 >= -n >= 50:
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
else:
print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

下面是您想要的函数:

def num_info(n: int) -> None:
print(f"=== SHORT DESCRIPTION OF {n} ===")
# even or odd
if n % 2:
print(f"{n} is an ODD NUMBER")
else:
print(f"{n} is an EVEN NUMBER")
# positive or negative
if n > 0:
print(f"{n} is a POSITIVE NUMBER")
elif n < 0:
print(f"{n} is a NEGATIVE NUMBER")
else:
print(f"{n} is NEITHER a POSITIVE nor a NEGATIVE NUMBER")
# divisible by 3
if n % 3:
print(f"{n} is not DIVISIBLE by 3")
else:
print(f"{n} is DIVISIBLE by 3")
# absolute value of n is between 50 and 99 (inclusive)
if 50 <= abs(n) <= 99:
print(f"The ABSOLUTE VALUE of {n} is between 50 and 99 INCLUSIVE")
else:
print(f"The ABSOLUTE VALUE of {n} is not between 50 and 99 INCLUSIVE")

你可以这样测试:

def main() -> None:
n = int(input("ENTER A NUMBER: "))
num_info(n)

if __name__ == "__main__":
main()

值得注意的是,您可以将函数的四个部分分解为各自独立的函数,以提高可读性,但在这样一个简单的示例中,这并不是真正必要的。

根据@Ran A的回答,您可以使用abs();再一次,不需要两次测试——只需要得到绝对值并进行检查。

但真正的问题是elif 99 <= n >= 50:行:您正在检查n是否大于99并且也大于50。这不是你想要的。老实说,一旦你使用了绝对值,这个测试就变得多余了,但让我们保留它

n_abs = abs(n)
if 99 >= n_abs >= 50:
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif 99 > n_abs or n_abs < 50: #this could simply be else:
print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

您可以使用"abs"函数:

if 99 >= abs(n) >= 50:
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif n < 0:
99 >= abs(n) >= 50
print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif 99 <= abs(n) >= 50:
print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

最新更新