我需要输入一个大于 2 的数字,然后取平方根,直到平方根小于 2。我需要一个打印语句,其中包括取数字平方根的次数以及输出。到目前为止我所拥有的是:
import math
input_num = float(input("Enter a number greater than two: "))
while input_num < 2:
input_num = float(input("Enter a number greater than two: "))
else:
sqrt_num = math.sqrt(input_num)
count = 1
while sqrt_num > 2:
sqrt_num = math.sqrt(sqrt_num)
count += 1
print(count, ": ", sqrt_num, sep = '')
随着输出:
Enter a number greater than two: 20
2: 2.114742526881128
3: 1.4542154334489537
我想包括计数 1 的第一次迭代。如何编写一个正确的循环,使其看起来像:
Enter a number greater than two: 20
1: 4.47213595499958
2: 2.114742526881128
3: 1.4542154334489537
尚方宝剑之说
相关分类