You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3 条件判断(if-else)程序编译错误求助

Fixing Your Python 3 if-else Compilation Errors

Let's walk through the issues in your code and fix them up:

Key Issues Identified

  • Variable Case Mismatch: You initialized your variable as lowercase n (n = int(input().strip())), but used uppercase N in every condition check. Python is case-sensitive, so it treats n and N as two entirely different variables. This will trigger a NameError since N was never defined.
  • Python 3 print Syntax: In Python 3, print is a built-in function, which means you need to wrap the output string in parentheses. Your code uses the old Python 2 syntax (print "Weird"), which will cause a syntax error.

Corrected Code

n = int(input().strip())
if n % 2 != 0:
    print("Weird")
else:
    if n >= 2 and n <= 5:
        print("Not Weird")
    elif n >= 6 and n <= 20:
        print("Weird")
    elif n > 20:
        print("Not Weird")

Quick Explanation

  • We replaced all instances of N with n to match the variable you defined.
  • Added parentheses around all print statements to comply with Python 3's function syntax.
  • The logic itself is correct once these syntax issues are fixed—your conditions for determining "Weird" vs "Not Weird" work as intended.

内容的提问来源于stack exchange,提问作者A Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 16:42:38