4
我無法隔離問題。該程序應該採用兩個整數並將其轉換爲科學記數法,然後將它們相乘。然而它打印了兩次科學概念。但是它會打印兩次信息。這個程序爲什麼會打印兩次?
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()
+1。當一個函數具有所謂的*副作用*時就是這種情況。你應該避免這種情況。而不是打印,返回字符串或一個或多個值。然後調用者可以決定如何處理結果。 – pepr