2012-10-21 2 views
4

문제를 격리 할 수 ​​없습니다. 이 프로그램은 두 개의 정수를 가져 와서 과학 표기법으로 변환 한 다음 곱합니다. 그러나 그것은 과학적 개념을 두 번 인쇄합니다. 그러나 정보를 두 번 인쇄합니다.왜이 프로그램이 이중으로 인쇄됩니까?

u=str(int(convert(s))*int(convert(t))) 

을 그리고 당신은 번호를 다시 변환 호출 :

def convert(s): 
    print("You typed " + s) 
    n=0 
    for c in s: 
     n=n+1 
     if n==1: 
      print("In scientific notation:"+str(c)+'.', end='') 
     if n!=1: 
      print(str(c),end='') 
    print('X 10^'+str(len(s)-1)) 
    return c 

def convert_product(u): 
    n=0 
    for c in u: 
     n=n+1 
     if n==1: 
      print("Product in scientific notation "+c+'.', end='') 
     if n!=1: 
      print(c, end='') 


def main(): 
    s=input("Please input your first number\n") 
    t=input("Please input your second number\n") 
    u=str(int(convert(s))*int(convert(t))) 
    convert(s) 
    convert(t) 
    convert_product(u) 
    print('X 10^' + str(len(s)+len(t)-2)) 
main() 

답변

3

이 줄을 변환 호출

convert(s) 
convert(t) 

을 그리고 변환 기능은 인쇄입니다. 따라서 이중 인쇄물을 가지고 있습니다.

+2

+1. 함수가 부작용 *이라고 불리는 경우입니다. 당신은 그것을 피해야합니다. 인쇄하는 대신 문자열 또는 하나 이상의 값을 반환하십시오. 그러면 발신자는 결과를 어떻게 처리할지 결정할 수 있습니다. – pepr

관련 문제