Python代码报SyntaxError: invalid syntax错误,请求修复(第3行报错)
Python代码语法错误修复
运行以下Python代码时,始终提示第3行出现SyntaxError: invalid syntax语法错误,修复方案如下:
原始代码
nterms = int(input("How many terms? ") n1, n2 = 0, 1 count = 0, if nterms <= 0: print("Please enter a positive integer") elif nterms == 1: print("sequence upto",nterms,":") print(n1) else print("sequence:") while count < nterms print(n1) nth = n1 + n2 n1 = n2 n2 = nth count += 1
存在的语法错误
- 第1行
input()函数缺少闭合括号 - 第3行
count = 0后多了逗号 elif代码块内的print语句缩进不一致else语句后缺少冒号while循环语句后缺少冒号
修复后的代码
nterms = int(input("How many terms? ")) n1, n2 = 0, 1 count = 0 if nterms <= 0: print("Please enter a positive integer") elif nterms == 1: print("sequence upto", nterms, ":") print(n1) else: print("sequence:") while count < nterms: print(n1) nth = n1 + n2 n1 = n2 n2 = nth count += 1
内容的提问来源于stack exchange,提问作者Arifah Ayuniyyah
相关产品推荐
相关产品推荐

