2014-10-07 3 views

답변

5

가 "의 in 운영자에게

if operator not in ('+', '-', '/') 
+0

.. 및 ['+ ','- ','/ '}'in Python 3.2+] (https://docs.python.org/3/whatsnew/3.2.html#optimizations) –

+0

@AshwiniChaudhary - Ooo ... python3.2 optimizer가 똑똑하다는 것을 몰랐습니다. 환상적입니다. – mgilson

+0

python 2에서도 잘 작동합니다. https://docs.python.org/2/reference/expressions.html#set-displays – srj

0

을 할 필요없이 : if operator in ('+', '-', '*', '/') 또는 찾고처럼 단순히 if operator in '+-*/' (감사 @kindall) 캐릭터 라인의 char 나는 개인적으로 덜 읽을 수 있지만 찾을 수 있습니다. * - +

ops = {'+': 'plus', '-': 'minus', '*': 'times', '/': 'div'} 
operator = '/' 
try: 
    print ops[operator] 
except KeyError: 
    print "Unknow operation (%s)" % (operator) 
+0

'연산자를 사용

Otherwize 당신은 (각 연산자에 대한 기능을 결합하는, 예를 들어) 그냥 예외를 사용하는 사전도를 사용/"'... 연산자는 모두 단일 문자이므로 목록을 사용할 필요가 없습니다. – kindall

관련 문제