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 uppercaseNin every condition check. Python is case-sensitive, so it treatsnandNas two entirely different variables. This will trigger aNameErrorsinceNwas never defined. - Python 3
printSyntax: In Python 3,printis 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
Nwithnto match the variable you defined. - Added parentheses around all
printstatements 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
相关产品推荐
相关产品推荐

