##s = 0 ##s = s + 1 ##s = s + 2 ##s = s + 3 ##s = s + 4 ##s = s + 5 ##s = s + 6 ##s = s + 7 ##s = s + 8 ##s = s + 9 ##s = s + 10 ##print("The sum is", s) ############整数求和############ ##s = 0 ##for n in range(1, 11): ## s = s + n ## ##print("The sum is", s) ############等差整数求和######### ##a = int(input("Start from: ")) ##b = int(input("End at: ")) ##c = int(input("Step: ")) ## ##s = 0 ##for n in range(a, b+1, c): ## s = s + n ## ##print("The sum is", s) ##########摄氏到华氏的转换######## ##begin = int(input("Start from: ")) ##end = int(input("End at: ")) ##step = int(input("Step: ")) ## ##for x in range(begin, end, step) : ## print(x, "->", x * 9 / 5 + 32) #############求阶乘############## ##n = int(input("Factorial for: ")) ## ##prod = 1 ##for i in range(2, n+1): ## prod = prod * i ## ##print("The factorial of", n, "is", prod) ############# 求阶乘, 重复3次 ############## ##for i in range(3): ## n = int(input("Factorial for: ")) ## ## prod = 1 ## for j in range(2, n+1): ## prod = prod * j ## ## print("The factorial of", n, "is", prod) ##########摄氏到华氏的转换, while 版本######## ##begin = int(input("Start from: ")) ##end = int(input("End at: ")) ##step = int(input("Step: ")) ## ##x = begin ##while x <= end: ## print(x, "->", x * 9 / 5 + 32) ## x = x + step #############求阶乘, while 版本############## ##n = int(input("Factorial for: ")) ## ##prod = 1 ##i = 2 ##while i <= n: ## prod = prod * i ## i = i + 1 ## ##print("The factorial of", n, "is", prod) #############求阶乘计算器,负数结束############## ##print("This is a factorial calculator. -1 to stop.") ##n = int(input("Factorial for: ")) ## ##while n >= 0: ## prod = 1 ## i = 2 ## while i <= n: ## prod = prod * i ## i = i + 1 ## ## print("The factorial of", n, "is", prod) ## ## n = int(input("Factorial for: ")) #############求平方根(XXX! 会无穷循环,不能终止)############# ##x = float(input("Square root for: ")) ## ##guess = 1.0 ## ##while guess * guess != x: ## guess = (guess + x/guess)/2 ## ##print(guess) #######(可以用 ctrl-c 中断计算) #############求平方根############# ### abs is a build-in standard function ##x = float(input("Square root for: ")) ## ##guess = 1.0 ## ##while abs(guess * guess - x) > 1e-8: ## guess = (guess + x/guess)/2 ## ##print(guess) #############求平方根,带显示过程########### ### abs is a build-in standard function x = float(input("Square root for: ")) guess = 1.0 n = 0 while abs(guess * guess - x) > 1e-12: guess = (guess + x/guess)/2 n = n + 1 print(n, guess) print(guess)