跳转至内容

Python 编程/循环/解决方案

来自维基教科书,开放世界中的开放书籍

< 返回问题

1. 编写一个程序,用素数计数。要求用户输入一个数字,然后打印出小于该数字的每个素数。

对于这个程序,我们将创建一个包含所有可能的数字列表,直到用户的输入,然后使用费马小定理测试每个数字,验证它是素数,然后将该素数添加到素数列表中。最后,我们打印素数列表。

注释:所有素数都满足费马小定理,但有些返回的结果是“伪素数”,它们不是素数(例如,341=11*31,在 python 中:(((2**341)-2)%341 == 0)). 有关寻找素数和证明素性的更多信息

primes = []
                    # Creates a list that we will throw our prime numbers into.  
user = 1 + int(raw_input("What number would you like to count primes up to?  "))
                    # 1 is added on to the user's number so that their number
                    # is counted.  
list = range(2,user)
                    # 0 and 1 are not prime, but our algorithm registers them
                    # as prime, so we start with two
for possibility in list:
    if ((2**possibility)-2)%possibility == 0:
                    # Our algorithm (Fermat's little theorem)  states
                    # that if "i" is an integer and i^p-i divides evenly
                    # into p, p is prime.  We are using 2 in place of i.
                    # See Python Programming/Basic Math for more info.  		
        isprime = True
        test = 2
        while test < possibility and isprime:
                    # Here we verify that the number is in fact prime by
                    # starting at 2 and dividing it by every number lower 
                    # than the potential prime.  See comment above.
            if (possibility%test) == 0:
                    # This ends the while loop causing the next number
                    # to be tested.
                isprime = False
            test = test + 1
        if isprime:
                    # We add each prime to the primes list.  
            primes.append(possibility)
print ("The prime numbers from 0 to", user-1, "are: ")
for possibility in primes:
    print possibility
                    # Print out all the primes!

2. 指示用户从 1 到 100 中选择一个任意数字,然后在七次尝试内猜出该数字。每次猜完之后,用户必须告诉您他们选择的数字是比您的猜测高、低还是等于您的猜测。

print "\nIn your head, pick a number between 1 and 100, \ni'll guess it with some hints:\n"
letsstart = raw_input("ready? input anything...\n")
uppers = 100
lowers = 0
guess = 50
boom = 0
for c in range(1,10):
	if boom == 0:
		print "guess: ",guess
		answer = raw_input("Was that\na. too low\nb. too high\nc.  thats right!\n")
		if answer == "c":
			print "I got it!  Thanks for playing.\n"
			boom = 1
		elif answer == "a":
			lowers = guess
			blah = int((uppers-guess)/2)
			guess = guess + blah
		elif answer == "b":
			uppers = guess
			blash = int((guess-lowers)/2)
			guess = guess-blash


"其他"

Python 的 9 年级作业是这样的,这是一个猜数字的游戏,包括了 python 的大多数方面。

print("welcome")
YourName = input("what is your name?")
print("hi",YourName,"do you want to play my game?")
import random
number = random.randrange(100)
guess =int(input("please guess the number"))
if guess == number:
    print("You've WON!!!!!")
elif guess > number:
    print("Too Big!")
else: 
     print("Too Low!")
while guess!= number:
    guess =int(input("please guess the number again"))
    if guess == number:
        print("You've Won!!")
    elif guess > number:
        print("too big")
    else:
        print("too low")
华夏公益教科书