给定一个整数,执行以下条件操作:如果是奇数,则打印奇怪如果是偶数且在包含范围内,则打印不奇怪如果是偶数且在包含范围内,则打印奇怪如果是偶数并且大于,打印Not Weird
if __name__ == '__main__':
n = int(input("Enter a number:"))
if n % 2 != 0:
print("Weird")
elif n % 2 == 0:
for n in range(2,5):
print("Not Weird")
elif n % 2 == 0:
for n in range(6,20):
print("Weird")
elif n% 2 == 0 && (n > 20):
print("Not Weird")
答案 0 :(得分:0)
for
是一个循环,range()
是一个生成器(实际上是immutable sequence),它不会按照您的想法执行。您需要查看更多if
和elif
语句,例如:
if __name__ == '__main__':
n = int(input("Enter a number:"))
if n % 2 != 0:
print("Weird")
else:
if 2 <= n <= 5:
print("Not Weird")
elif 6 <= n <= 20:
print("Weird")
elif n > 20:
print("Not Weird")
else:
print("n = 0 or even-and-negative")