首页 > 解决方案 > Python-语法错误“if语句”

问题描述

我不断收到一个相同的错误消息(即使昨天一切正常)。从那时起,代码中没有任何变化。

这是代码本身:

x = [1, 4, 34, 56, 8, 5, 8, 6, 5, 12, 89, 23]  

z = int(input('Please eneter a number to check: ')    
if x.count(z) >= **2 :** *This is where it gives me a syntax error*        
    print('There are:', x.count(z), 'numbers of', z, 'in the list')    
elif x.count(z) > 0 < 2:        
    print('This number is unique in the list')
else:
    print('There is no such number in the list')

任何想法为什么会发生?

标签: pythonpython-3.x

解决方案


我不确定您遇到了什么语法错误,因为您没有明确说明。但我相信你错过了 z 变量的右括号。这是固定代码,它对我有用:

x = [1, 4, 34, 56, 8, 5, 8, 6, 5, 12, 89, 23]

z = int(input('Please eneter a number to check: '))

if x.count(z) >= 2 :
    print('There are:', x.count(z), 'numbers of', z, 'in the list')
elif x.count(z) > 0 < 2:
    print('This number is unique in the list')
else:
    print('There is no such number in the list')

推荐阅读