2014-07-07 5 views
0

이 오류를 검색했지만 답변을 찾지 못했습니다. 간단한 CALC 프로그램 :정수를 곱하는 경우 Python ValueError

import sys 
from sys import argv 

first = int(sys.argv[1]) 
operation = sys.argv[2] 
second = int(sys.argv[3]) 

if operation == '+': 
    total = first + second   
if operation == '-': 
    total = first - second 
if operation == '*': 
    total = first * second 
if operation == '/': 
    total = first/second 

print "%d %s %d = %d" % (first, operation, second, total) 

I 입력 : python first.py 2/2 내가 -+와 같은 올바른 출력을 얻을,하지만 난 python first.py 2 * 2를 입력 할 때 내가 얻을 :

Traceback (most recent call last): 
    File "first.py", line 7, in <module> 
    second = int(sys.argv[3]) 
ValueError: invalid literal for int() with base 10: 'first.py'` 

답변

3

*쉘 메타 문자입니다 의미 : 현재 디렉토리의 모든 파일을 여기에 나열하십시오. 쉘이 먼저 모든 파일 이름에 *, 그 이름과 파이썬이라는 다음 확장했기 때문에 그 결과

, sys.argv['first.py', '2', '*', '2']에 있지만 ['first.py', '2', 'some-filename.txt', 'first.py', 'another-filename.py', '2'] 또는 유사한 설정되지 않습니다.

은 쉘에서 * 탈출

:

python first.py 2 \* 2 
+0

이 아, 잡았다, 당신에게 선생님 감사합니다. – user3763437

관련 문제