我正在做一個大學項目,開始打印出兩個給定輸入之間的所有素數。後來我被告知它必須與我的課程網絡管理有些相關,所以我想在我的腳本的末尾添加一個密碼生成器(用於網絡安全)使用素數生成一個隨機密碼
我已將所有代碼寫出來,但是我有一個問題,它不能使用我打印出來的列表中的隨機素數。它只使用最後打印的數字,我理解爲什麼,但無論如何,我可以做到這一點,使用隨機素數或我將不得不在某處存儲數字?
#A program to count the prime numbers from a given start to a given end
#importing math function
import math
import os, random, string
#Input the number to start counting from
Starting_number = input("Enter the starting number: ")
#Input the number to end the count on.
Ending_number = input("Enter the number you want to count up to: ")
#if Starting_number is less than 0 it will print out a suitable message.
if Starting_number < 0:
print 'Invalid entry, please enter a positiv number. \nWill count from ',Starting_number, 'to 0 and begin prime number count to',Ending_number, '.'
#If Ending_number is less than or equals to 0 then it will print out a suitable message.
if Ending_number <= 0:
print 'Invalid entry on last input \nPlease enter two positive numbers for the count to work.'
#Starting loop as long as the current count is between Starting_number and Ending_number
for num in range(Starting_number, Ending_number):
#
if all(num%i !=0 for i in range(2,num)):
print num
if num >= 1 and num <= 100:
length = 4
chars = string.ascii_letters + string.digits + '[email protected]#$%^&*()'
random.seed = (os.urandom(1024))
print ''.join(random.choice(chars) for i in range(length))
if num >= 101 and num <= 200:
length = (Ending_number/Starting_number) * 5 + 11
if length >= num:
length = num/100
chars = string.ascii_letters + string.digits + '[email protected]#$%^&*()'
random.seed = (os.urandom(1024))
print ''.join(random.choice(chars) for i in range(length))
避免在python2中輸入。它不會*將數字作爲輸入。它*執行* python代碼。如果在提示輸入起始號碼時嘗試輸入'[1,2,3]',則會看到一個您不期望的錯誤。用戶也可以插入'__import __(「os」)。system(「kill my machine command」)這樣的東西,並且可能會發生非常糟糕的事情。要請求一個整數,只需使用'int(raw_input(...))'。或浮點數(raw_input(...))'浮點數。在python3中,名爲'input'的函數實際上是'raw_input',因此無論如何您都必須添加int(..)'調用。 – Bakuriu