n = input('please input a number:')字符串
這樣默認接收的是字符串,input
如下代碼就會報錯:while
n = input('please input a number:')
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1co
print("1 到 %d 之和爲: %d" % (n,sum))字符
改成如下:
n = int(input('please input a number:'))
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1
print("1 到 %d 之和爲: %d" % (n,sum))