打印的Python问题

时间:2018-02-28 15:35:50

标签: python

今天我选择学习一门新语言Python。我对C有一些了解。 我开始在我的iPad上使用Pythonista 3(在旅途中),现在我回家了,复制了我的代码,我遇到了以下问题:

#!/use/bin/python

startNumber = 3 #1000329977989
endNumber = startNumber + 100 #int(input('Enter the ending number:'))
numberToTest = startNumber

string = '1 is prime!'
print(string)
string = '2 is prime!'
print(string)
string = '3 is prime!'
print(string)

while numberToTest < endNumber -1:
    for numberToTest in range(startNumber, endNumber):
        testerRangeEnd = int(numberToTest**(1/2))
        for tester in range(2, testerRangeEnd):
            if numberToTest % tester == 0:
                string = str(numberToTest) + ' is not prime!'
                print(string)
                numberToTest += 1
                break
            if tester == testerRangeEnd - 1:
                string = str(numberToTest) + ' is prime!'
                print(string)

print("Done!")

输出:

1 is prime!
2 is prime!
3 is prime!
Done!

为什么while循环中的打印件不能打印?我根本得不到它...错过我的括号,分号和括号...... :(

干杯 / Leic

1 个答案:

答案 0 :(得分:0)

看看这一行:

testerRangeEnd = int(numberToTest**(1/2))

1/2给出0,因为它是整数除法。因此for tester in range(2, testerRangeEnd):等于iteration for tester in []:并且迭代不会开始。

您应该将此数字转换为浮点数:1.0/2 = 0.5