Python 小型项目大全 21~25

news2025/6/16 15:59:50

二十一、DNA 可视化

原文:http://inventwithpython.com/bigbookpython/project21.html

脱氧核糖核酸是一种微小的分子,存在于我们身体的每个细胞中,包含着我们身体如何生长的蓝图。它看起来像一对核苷酸分子的双螺旋结构:鸟嘌呤、胞嘧啶、腺嘌呤和胸腺嘧啶。这些用字母 G、C、A 和 T 来表示。DNA 是一个长分子;它是微观的,但是如果把它拉长,它的 30 亿个碱基对会有 2 米长!这个程序是一个简单的 DNA 动画。

运行示例

当您运行dna.py时,输出将如下所示:

DNA Animation, by Al Sweigart email@protected
Press Ctrl-C to quit...
        #G-C#
       #C---G#
      #T-----A#
     #T------A#
    #A------T#
    #G-----C#
     #G---C#
     #C-G#
      ##
     #T-A#
     #C---G#
    #G-----C#
    #G------C#
     #T------A#
      #A-----T#
       #C---G#
        #G-C#
         ##
        #T-A#
       #T---A#
      #A-----T#
`--snip--`

工作原理

与项目 15“深坑”和项目 20“数字雨”类似,这个程序通过打印ROWS列表中的字符串来创建滚动动画。使用format()字符串方法将 AT 和 CG 对插入到每个字符串中。

"""DNA, by Al Sweigart email@protected
A simple animation of a DNA double-helix. Press Ctrl-C to stop.
Inspired by matoken https://asciinema.org/a/155441
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: short, artistic, scrolling, science"""

import random, sys, time

PAUSE = 0.15  # (!) Try changing this to 0.5 or 0.0.

# These are the individual rows of the DNA animation:
ROWS = [
    #123456789 <- Use this to measure the number of spaces:
    '         ##',  # Index 0 has no {}.
    '        #{}-{}#',
    '       #{}---{}#',
    '      #{}-----{}#',
    '     #{}------{}#',
    '    #{}------{}#',
    '    #{}-----{}#',
    '     #{}---{}#',
    '     #{}-{}#',
    '      ##',  # Index 9 has no {}.
    '     #{}-{}#',
    '     #{}---{}#',
    '    #{}-----{}#',
    '    #{}------{}#',
    '     #{}------{}#',
    '      #{}-----{}#',
    '       #{}---{}#',
    '        #{}-{}#']
    #123456789 <- Use this to measure the number of spaces:

try:
    print('DNA Animation, by Al Sweigart email@protected')
    print('Press Ctrl-C to quit...')
    time.sleep(2)
    rowIndex = 0

    while True:  # Main program loop.
        # Increment rowIndex to draw next row:
        rowIndex = rowIndex + 1
        if rowIndex == len(ROWS):
            rowIndex = 0

        # Row indexes 0 and 9 don't have nucleotides:
        if rowIndex == 0 or rowIndex == 9:
            print(ROWS[rowIndex])
            continue

        # Select random nucleotide pairs, guanine-cytosine and
        # adenine-thymine:
        randomSelection = random.randint(1, 4)
        if randomSelection == 1:
            leftNucleotide, rightNucleotide = 'A', 'T'
        elif randomSelection == 2:
            leftNucleotide, rightNucleotide = 'T', 'A'
        elif randomSelection == 3:
            leftNucleotide, rightNucleotide = 'C', 'G'
        elif randomSelection == 4:
            leftNucleotide, rightNucleotide = 'G', 'C'

        # Print the row.
        print(ROWS[rowIndex].format(leftNucleotide, rightNucleotide))
        time.sleep(PAUSE)  # Add a slight pause.
except KeyboardInterrupt:
    sys.exit()  # When Ctrl-C is pressed, end the program. 

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果把第 42 行的rowIndex = rowIndex + 1改成rowIndex = rowIndex + 2会怎么样?
  2. 如果把 53 行的random.randint(1, 4)改成random.randint(1, 2)会怎么样?
  3. 如果将第 9 行的PAUSE = 0.15设置为PAUSE = -0.15,会得到什么错误信息?

二十二、小鸭子

原文:http://inventwithpython.com/bigbookpython/project22.html

这个程序创建了一个滚动的小鸭场。每只小鸭子都略有不同:它们可以面向左边或右边,有两种不同的体型,四种眼睛,两种嘴巴,三种翅膀位置。这给了我们 96 种不同的可能变异,这些变异是小鸭程序不断产生的。

运行示例

当你运行ducklings.py的时候,输出将如下所示:

Duckling Screensaver, by Al Sweigart email@protected
Press Ctrl-C to quit...
                                             =" )
=")                                          (  v)=")
( ^)                                          ^ ^ ( v) >'')
 ^^                                                ^^  (  ^)
                              >")                       ^ ^
                              ( v)      =^^)
 ("<  ("<                >")   ^^       (  >)
(^ ) (< )                ( ^)            ^ ^
 ^^   ^^             ("<  ^^                       (``<>^^)
 (^^=               (^ )                          (<  )(  ^)
(v  ) ( "<           ^^                            ^ ^  ^ ^
`--snip--`

工作原理

这个程序用一个Duckling类表示小鸭子。在这个类的__init__()方法中选择了每只鸭子的随机特征,而每只鸭子的各个身体部分由getHeadStr()getBodyStr()getFeetStr()方法返回。

"""Duckling Screensaver, by Al Sweigart email@protected
A screensaver of many many ducklings.

>" )   =^^)    (``=   ("=  >")    ("=
(  >)  (  ^)  (v  )  (^ )  ( >)  (v )
^ ^    ^ ^    ^ ^    ^^    ^^    ^^

This code is available at https://nostarch.com/big-book-small-python-programming
Tags: large, artistic, object-oriented, scrolling"""

import random, shutil, sys, time

# Set up the constants:
PAUSE = 0.2  # (!) Try changing this to 1.0 or 0.0.
DENSITY = 0.10  # (!) Try changing this to anything from 0.0 to 1.0.

DUCKLING_WIDTH = 5
LEFT = 'left'
RIGHT = 'right'
BEADY = 'beady'
WIDE = 'wide'
HAPPY = 'happy'
ALOOF = 'aloof'
CHUBBY = 'chubby'
VERY_CHUBBY = 'very chubby'
OPEN = 'open'
CLOSED = 'closed'
OUT = 'out'
DOWN = 'down'
UP = 'up'
HEAD = 'head'
BODY = 'body'
FEET = 'feet'

# Get the size of the terminal window:
WIDTH = shutil.get_terminal_size()[0]
# We can't print to the last column on Windows without it adding a
# newline automatically, so reduce the width by one:
WIDTH -= 1


def main():
   print('Duckling Screensaver, by Al Sweigart')
   print('Press Ctrl-C to quit...')
   time.sleep(2)

   ducklingLanes = [None] * (WIDTH // DUCKLING_WIDTH)

   while True:  # Main program loop.
       for laneNum, ducklingObj in enumerate(ducklingLanes):
           # See if we should create a duckling in this lane:
           if (ducklingObj == None and random.random() <= DENSITY):
                   # Place a duckling in this lane:
                   ducklingObj = Duckling()
                   ducklingLanes[laneNum] = ducklingObj

           if ducklingObj != None:
               # Draw a duckling if there is one in this lane:
               print(ducklingObj.getNextBodyPart(), end='')
               # Delete the duckling if we've finished drawing it:
               if ducklingObj.partToDisplayNext == None:
                   ducklingLanes[laneNum] = None
           else:
               # Draw five spaces since there is no duckling here.
               print(' ' * DUCKLING_WIDTH, end='')

       print()  # Print a newline.
       sys.stdout.flush()  # Make sure text appears on the screen.
       time.sleep(PAUSE)


class Duckling:
   def __init__(self):
       """Create a new duckling with random body features."""
       self.direction = random.choice([LEFT, RIGHT])
       self.body = random.choice([CHUBBY, VERY_CHUBBY])
       self.mouth = random.choice([OPEN, CLOSED])
       self.wing = random.choice([OUT, UP, DOWN])

       if self.body == CHUBBY:
           # Chubby ducklings can only have beady eyes.
           self.eyes = BEADY
       else:
           self.eyes = random.choice([BEADY, WIDE, HAPPY, ALOOF])

       self.partToDisplayNext = HEAD

   def getHeadStr(self):
       """Returns the string of the duckling's head."""
       headStr = ''
       if self.direction == LEFT:
           # Get the mouth:
           if self.mouth == OPEN:
               headStr += '>'
           elif self.mouth == CLOSED:
               headStr += '='

           # Get the eyes:
           if self.eyes == BEADY and self.body == CHUBBY:
                headStr += '"'
            elif self.eyes == BEADY and self.body == VERY_CHUBBY:
                headStr += '" '
            elif self.eyes == WIDE:
                headStr += "''"
            elif self.eyes == HAPPY:
                headStr += '^^'
            elif self.eyes == ALOOF:
                headStr += '``'

            headStr += ') '  # Get the back of the head.

        if self.direction == RIGHT:
            headStr += ' ('  # Get the back of the head.

            # Get the eyes:
            if self.eyes == BEADY and self.body == CHUBBY:
                headStr += '"'
            elif self.eyes == BEADY and self.body == VERY_CHUBBY:
                headStr += ' "'
            elif self.eyes == WIDE:
                headStr += "''"
            elif self.eyes == HAPPY:
                headStr += '^^'
            elif self.eyes == ALOOF:
                headStr += '``'

            # Get the mouth:
            if self.mouth == OPEN:
                headStr += '<'
            elif self.mouth == CLOSED:
                headStr += '='

        if self.body == CHUBBY:
            # Get an extra space so chubby ducklings are the same
            # width as very chubby ducklings.
            headStr += ' '

        return headStr

    def getBodyStr(self):
        """Returns the string of the duckling's body."""
        bodyStr = '('  # Get the left side of the body.
        if self.direction == LEFT:
            # Get the interior body space:
            if self.body == CHUBBY:
                bodyStr += ' '
            elif self.body == VERY_CHUBBY:
                bodyStr += '  '

            # Get the wing:
            if self.wing == OUT:
                bodyStr += '>'
            elif self.wing == UP:
                bodyStr += '^'
            elif self.wing == DOWN:
                bodyStr += 'v'

        if self.direction == RIGHT:
            # Get the wing:
            if self.wing == OUT:
                bodyStr += '<'
            elif self.wing == UP:
                bodyStr += '^'
            elif self.wing == DOWN:
                bodyStr += 'v'

            # Get the interior body space:
            if self.body == CHUBBY:
                bodyStr += ' '
            elif self.body == VERY_CHUBBY:
                bodyStr += '  '

        bodyStr += ')'  # Get the right side of the body.

        if self.body == CHUBBY:
            # Get an extra space so chubby ducklings are the same
            # width as very chubby ducklings.
            bodyStr += ' '

        return bodyStr

    def getFeetStr(self):
        """Returns the string of the duckling's feet."""
        if self.body == CHUBBY:
            return ' ^^  '
        elif self.body == VERY_CHUBBY:
            return ' ^ ^ '

    def getNextBodyPart(self):
        """Calls the appropriate display method for the next body
        part that needs to be displayed. Sets partToDisplayNext to
        None when finished."""
        if self.partToDisplayNext == HEAD:
            self.partToDisplayNext = BODY
            return self.getHeadStr()
        elif self.partToDisplayNext == BODY:
            self.partToDisplayNext = FEET
            return self.getBodyStr()
        elif self.partToDisplayNext == FEET:
            self.partToDisplayNext = None
            return self.getFeetStr()



# If this program was run (instead of imported), run the game:
if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        sys.exit()  # When Ctrl-C is pressed, end the program. 

在输入源代码并运行几次之后,尝试对其进行实验性的修改。标有(!)的注释对你可以做的小改变有建议。

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果将第 75 行的random.choice([LEFT, RIGHT])改为random.choice([LEFT])?会发生什么
  2. 如果把 194 行的self.partToDisplayNext = BODY改成self.partToDisplayNext = None会怎么样?
  3. 如果把 197 行的self.partToDisplayNext = FEET改成self.partToDisplayNext = BODY会怎么样?
  4. 如果把 195 行的return self.getHeadStr()改成return self.getFeetStr()会怎么样?

二十三、蚀刻绘图器

原文:http://inventwithpython.com/bigbookpython/project23.html

当您使用WASD键在屏幕上移动笔尖时,蚀刻绘图器通过描绘一条连续的线来形成一幅图片,就像蚀刻素描玩具一样。让你艺术的一面爆发出来,看看你能创造出什么形象!这个程序还可以让你把你的绘图保存到一个文本文件中,这样你以后就可以把它们打印出来。此外,你可以将其他图形的WASD运动复制粘贴到这个程序中,比如源代码第 6 到 14 行中的希尔伯特曲线分形的WASD命令。

运行示例

当你运行etchingdrawer.py时,输出将看起来像图 23-1 。

f23001

:在蚀刻绘图器程序中绘制的图

工作原理

与项目 17“骰子数学”一样,这个程序使用存储在名为canvas的变量中的字典来记录绘图的线条。关键字是(x, y)元组,值是表示要在屏幕上的 x,y 坐标处绘制的线形的字符串。附录 b 给出了可以在 Python 程序中使用的 Unicode 字符的完整列表。

第 126 行有一个对open()的函数调用,它传递了一个encoding='utf-8'参数。原因超出了本书的范围,但这是 Windows 将行字符写入文本文件所必需的。

"""Etching Drawer, by Al Sweigart email@protected
An art program that draws a continuous line around the screen using the
WASD keys. Inspired by Etch A Sketch toys.

For example, you can draw Hilbert Curve fractal with:
SDWDDSASDSAAWASSDSASSDWDSDWWAWDDDSASSDWDSDWWAWDWWASAAWDWAWDDSDW

Or an even larger Hilbert Curve fractal with:
DDSAASSDDWDDSDDWWAAWDDDDSDDWDDDDSAASDDSAAAAWAASSSDDWDDDDSAASDDSAAAAWA
ASAAAAWDDWWAASAAWAASSDDSAASSDDWDDDDSAASDDSAAAAWAASSDDSAASSDDWDDSDDWWA
AWDDDDDDSAASSDDWDDSDDWWAAWDDWWAASAAAAWDDWAAWDDDDSDDWDDSDDWDDDDSAASDDS
AAAAWAASSDDSAASSDDWDDSDDWWAAWDDDDDDSAASSDDWDDSDDWWAAWDDWWAASAAAAWDDWA
AWDDDDSDDWWAAWDDWWAASAAWAASSDDSAAAAWAASAAAAWDDWAAWDDDDSDDWWWAASAAAAWD
DWAAWDDDDSDDWDDDDSAASSDDWDDSDDWWAAWDD

This code is available at https://nostarch.com/big-book-small-python-programming
Tags: large, artistic"""

import shutil, sys

# Set up the constants for line characters:
UP_DOWN_CHAR         = chr(9474)  # Character 9474 is '│'
LEFT_RIGHT_CHAR      = chr(9472)  # Character 9472 is '─'
DOWN_RIGHT_CHAR      = chr(9484)  # Character 9484 is '┌'
DOWN_LEFT_CHAR       = chr(9488)  # Character 9488 is '┐'
UP_RIGHT_CHAR        = chr(9492)  # Character 9492 is '└'
UP_LEFT_CHAR         = chr(9496)  # Character 9496 is '┘'
UP_DOWN_RIGHT_CHAR   = chr(9500)  # Character 9500 is '├'
UP_DOWN_LEFT_CHAR    = chr(9508)  # Character 9508 is '┤'
DOWN_LEFT_RIGHT_CHAR = chr(9516)  # Character 9516 is '┬'
UP_LEFT_RIGHT_CHAR   = chr(9524)  # Character 9524 is '┴'
CROSS_CHAR           = chr(9532)  # Character 9532 is '┼'
# A list of chr() codes is at https://inventwithpython.com/chr

# Get the size of the terminal window:
CANVAS_WIDTH, CANVAS_HEIGHT = shutil.get_terminal_size()
# We can't print to the last column on Windows without it adding a
# newline automatically, so reduce the width by one:
CANVAS_WIDTH -= 1
# Leave room at the bottom few rows for the command info lines.
CANVAS_HEIGHT -= 5

"""The keys for canvas will be (x, y) integer tuples for the coordinate,
and the value is a set of letters W, A, S, D that tell what kind of line
should be drawn."""
canvas = {}
cursorX = 0
cursorY = 0


def getCanvasString(canvasData, cx, cy):
   """Returns a multiline string of the line drawn in canvasData."""
   canvasStr = ''

   """canvasData is a dictionary with (x, y) tuple keys and values that
   are sets of 'W', 'A', 'S', and/or 'D' strings to show which
   directions the lines are drawn at each xy point."""
   for rowNum in range(CANVAS_HEIGHT):
       for columnNum in range(CANVAS_WIDTH):
           if columnNum == cx and rowNum == cy:
               canvasStr += '#'
               continue

           # Add the line character for this point to canvasStr.
           cell = canvasData.get((columnNum, rowNum))
           if cell in (set(['W', 'S']), set(['W']), set(['S'])):
               canvasStr += UP_DOWN_CHAR
           elif cell in (set(['A', 'D']), set(['A']), set(['D'])):
               canvasStr += LEFT_RIGHT_CHAR
           elif cell == set(['S', 'D']):
               canvasStr += DOWN_RIGHT_CHAR
           elif cell == set(['A', 'S']):
               canvasStr += DOWN_LEFT_CHAR
           elif cell == set(['W', 'D']):
               canvasStr += UP_RIGHT_CHAR
           elif cell == set(['W', 'A']):
               canvasStr += UP_LEFT_CHAR
           elif cell == set(['W', 'S', 'D']):
               canvasStr += UP_DOWN_RIGHT_CHAR
           elif cell == set(['W', 'S', 'A']):
               canvasStr += UP_DOWN_LEFT_CHAR
           elif cell == set(['A', 'S', 'D']):
               canvasStr += DOWN_LEFT_RIGHT_CHAR
           elif cell == set(['W', 'A', 'D']):
               canvasStr += UP_LEFT_RIGHT_CHAR
           elif cell == set(['W', 'A', 'S', 'D']):
               canvasStr += CROSS_CHAR
           elif cell == None:
               canvasStr += ' '
       canvasStr += '\n'  # Add a newline at the end of each row.
   return canvasStr


moves = []
while True:  # Main program loop.
   # Draw the lines based on the data in canvas:
   print(getCanvasString(canvas, cursorX, cursorY))

   print('WASD keys to move, H for help, C to clear, '
        + 'F to save, or QUIT.')
    response = input('> ').upper()

    if response == 'QUIT':
        print('Thanks for playing!')
        sys.exit()  # Quit the program.
    elif response == 'H':
        print('Enter W, A, S, and D characters to move the cursor and')
        print('draw a line behind it as it moves. For example, ddd')
        print('draws a line going right and sssdddwwwaaa draws a box.')
        print()
        print('You can save your drawing to a text file by entering F.')
        input('Press Enter to return to the program...')
        continue
    elif response == 'C':
        canvas = {}  # Erase the canvas data.
        moves.append('C')  # Record this move.
    elif response == 'F':
        # Save the canvas string to a text file:
        try:
            print('Enter filename to save to:')
            filename = input('> ')

            # Make sure the filename ends with .txt:
            if not filename.endswith('.txt'):
                filename += '.txt'
            with open(filename, 'w', encoding='utf-8') as file:
                file.write(''.join(moves) + '\n')
                file.write(getCanvasString(canvas, None, None))
        except:
            print('ERROR: Could not save file.')

    for command in response:
        if command not in ('W', 'A', 'S', 'D'):
            continue  # Ignore this letter and continue to the next one.
        moves.append(command)  # Record this move.

        # The first line we add needs to form a full line:
        if canvas == {}:
            if command in ('W', 'S'):
                # Make the first line a horizontal one:
                canvas[(cursorX, cursorY)] = set(['W', 'S'])
            elif command in ('A', 'D'):
                # Make the first line a vertical one:
                canvas[(cursorX, cursorY)] = set(['A', 'D'])

        # Update x and y:
        if command == 'W' and cursorY > 0:
            canvas[(cursorX, cursorY)].add(command)
            cursorY = cursorY - 1
        elif command == 'S' and cursorY < CANVAS_HEIGHT - 1:
            canvas[(cursorX, cursorY)].add(command)
            cursorY = cursorY + 1
        elif command == 'A' and cursorX > 0:
            canvas[(cursorX, cursorY)].add(command)
            cursorX = cursorX - 1
        elif command == 'D' and cursorX < CANVAS_WIDTH - 1:
            canvas[(cursorX, cursorY)].add(command)
            cursorX = cursorX + 1
        else:
            # If the cursor doesn't move because it would have moved off
            # the edge of the canvas, then don't change the set at
            # canvas[(cursorX, cursorY)].
            continue

        # If there's no set for (cursorX, cursorY), add an empty set:
        if (cursorX, cursorY) not in canvas:
            canvas[(cursorX, cursorY)] = set()

        # Add the direction string to this xy point's set:
        if command == 'W':
            canvas[(cursorX, cursorY)].add('S')
        elif command == 'S':
            canvas[(cursorX, cursorY)].add('W')
        elif command == 'A':
            canvas[(cursorX, cursorY)].add('D')
        elif command == 'D':
            canvas[(cursorX, cursorY)].add('A') 

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果把 101 行的response = input('> ').upper()改成response = input('> ')会怎么样?
  2. 如果把第 61 行的canvasStr += '#'改成canvasStr += '@'会怎么样?
  3. 如果把第 89 行的canvasStr += ' '改成canvasStr += '.'会怎么样?
  4. 如果把 94 行的moves = []改成moves = list('SDWDDSASDSAAWASSDSAS')会怎么样?

二十四、因子查找器

原文:http://inventwithpython.com/bigbookpython/project24.html

一个数的因数是任何两个其它数,当它们彼此相乘时,产生该数。比如2 × 13 = 26,所以 2 和 13 是 26 的因数。还有,1 × 26 = 26,所以 1 和 26 也是 26 的因数。所以我们说 26 有四个因子:1,2,13,26。

如果一个数只有两个因子(1 和它本身),我们称之为质数。否则,我们称之为合数。使用因数查找器发现一些新的质数!(提示:质数总是以不是 5 的奇数结尾。)你也可以让计算机用项目 56“质数”来计算它们

这个程序的数学并不太重,这使它成为初学者的理想项目。

运行示例

当您运行factorfinder.py时,输出将如下所示:

Factor Finder, by Al Sweigart email@protected
`--snip--`
Enter a number to factor (or "QUIT" to quit):
> 26
1, 2, 13, 26
Enter a number to factor (or "QUIT" to quit):
> 4352784
1, 2, 3, 4, 6, 8, 12, 16, 24, 29, 48, 53, 58, 59, 87, 106, 116, 118, 159, 174, 177, 212, 232, 236, 318, 348, 354, 424, 464, 472, 636, 696, 708, 848, 944, 1272, 1392, 1416, 1537, 1711, 2544, 2832, 3074, 3127, 3422, 4611, 5133, 6148, 6254, 6844, 9222, 9381, 10266, 12296, 12508, 13688, 18444, 18762, 20532, 24592, 25016, 27376, 36888, 37524, 41064, 50032, 73776, 75048, 82128, 90683, 150096, 181366, 272049, 362732, 544098, 725464, 1088196, 1450928, 2176392, 4352784
Enter a number to factor (or "QUIT" to quit):
> 9787
1, 9787
Enter a number to factor (or "QUIT" to quit):
> quit

工作原理

我们可以通过检查第二个数是否能整除第一个数来判断一个数是否是另一个数的因数。例如,7 是 21 的因数,因为 21 ÷ 7 是 3。这也给了我们 21 的另一个因素:3。但是,8 不是 21 的因数,因为 21 ÷ 8 = 2.625。分数余数部分告诉我们这个等式没有被均匀地划分。

%模数操作符将执行除法并告诉我们是否有余数:21 % 7计算为0,意味着没有余数,7 是 21 的因数,而21 % 8计算为1,一个非零值,意味着它不是一个因数。因子查找程序在第 35 行使用这种技术来确定哪些数字是因子。

math.sqrt()函数返回传递给它的数字的平方根。例如,math.sqrt(25)返回5.0,因为 5 的倍数本身就是 25,所以它是 25 的平方根。

"""Factor Finder, by Al Sweigart email@protected
Finds all the factors of a number.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, math"""

import math, sys

print('''Factor Finder, by Al Sweigart email@protected

A number's factors are two numbers that, when multiplied with each
other, produce the number. For example, 2 x 13 = 26, so 2 and 13 are
factors of 26\. 1 x 26 = 26, so 1 and 26 are also factors of 26\. We
say that 26 has four factors: 1, 2, 13, and 26.

If a number only has two factors (1 and itself), we call that a prime
number. Otherwise, we call it a composite number.

Can you discover some prime numbers?
''')

while True:  # Main program loop.
    print('Enter a positive whole number to factor (or QUIT):')
    response = input('> ')
    if response.upper() == 'QUIT':
        sys.exit()

    if not (response.isdecimal() and int(response) > 0):
        continue
    number = int(response)

    factors = []

    # Find the factors of number:
    for i in range(1, int(math.sqrt(number)) + 1):
        if number % i == 0:  # If there's no remainder, it is a factor.
            factors.append(i)
            factors.append(number // i)

    # Convert to a set to get rid of duplicate factors:
    factors = list(set(factors))
    factors.sort()

    # Display the results:
    for i, factor in enumerate(factors):
        factors[i] = str(factor)
    print(', '.join(factors)) 

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果删除或注释掉第 36 行的factors.append(i)会发生什么?
  2. 如果删除或注释掉第 40 行的factors = list(set(factors))会发生什么?(提示:输入一个平方数,如 25 或 36 或 49。)
  3. 如果删除或注释掉第 41 行的factors.sort()会发生什么?
  4. 如果将第 31 行的factors = []改为factors = '',会得到什么错误信息?
  5. 如果把第 31 行的factors = []改成factors = [-42]会怎么样?
  6. 如果将第 31 行的factors = []改为factors = ['hello'],会得到什么错误信息?

二十五、快速反应

原文:http://inventwithpython.com/bigbookpython/project25.html

这个程序测试你的反应速度:一看到DRAW这个词就按回车。但是要小心。在DRAW出现之前按下它,你就输了。你是西方最快的键盘吗?

运行示例

当您运行fastdraw.py时,输出将如下所示:

Fast Draw, by Al Sweigart email@protected

Time to test your reflexes and see if you are the fastest
draw in the west!
When you see "DRAW", you have 0.3 seconds to press Enter.
But you lose if you press Enter before "DRAW" appears.

Press Enter to begin...

It is high noon...
DRAW!

You took 0.3485 seconds to draw. Too slow!
Enter QUIT to stop, or press Enter to play again.
> quit
Thanks for playing!

工作原理

input()函数在等待用户输入字符串时暂停程序。这个简单的行为意味着我们不能只用input()来创建实时游戏。然而,你的程序将缓冲键盘输入,这意味着如果你在input()被调用之前按下CAT键,这些字符将被保存,一旦input()执行,它们将立即出现。

通过记录第 22 行的input()调用之前的时间和第 24 行的input()调用之后的时间,我们可以确定玩家按下回车花了多长时间。然而,如果他们在调用input()之前按下回车,回车按下的缓冲会导致input()立即返回(通常在大约 3 毫秒内)。这就是为什么第 26 行检查时间是否小于 0.01 秒或 10 毫秒,以确定玩家按下回车太快。

"""Fast Draw, by Al Sweigart email@protected
Test your reflexes to see if you're the fastest draw in the west.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, game"""

import random, sys, time

print('Fast Draw, by Al Sweigart email@protected')
print()
print('Time to test your reflexes and see if you are the fastest')
print('draw in the west!')
print('When you see "DRAW", you have 0.3 seconds to press Enter.')
print('But you lose if you press Enter before "DRAW" appears.')
print()
input('Press Enter to begin...')

while True:
    print()
    print('It is high noon...')
    time.sleep(random.randint(20, 50) / 10.0)
    print('DRAW!')
    drawTime = time.time()
    input()  # This function call doesn't return until Enter is pressed.
    timeElapsed = time.time() - drawTime

    if timeElapsed < 0.01:
        # If the player pressed Enter before DRAW! appeared, the input()
        # call returns almost instantly.
        print('You drew before "DRAW" appeared! You lose.')
    elif timeElapsed > 0.3:
        timeElapsed = round(timeElapsed, 4)
        print('You took', timeElapsed, 'seconds to draw. Too slow!')
    else:
        timeElapsed = round(timeElapsed, 4)
        print('You took', timeElapsed, 'seconds to draw.')
        print('You are the fastest draw in the west! You win!')

    print('Enter QUIT to stop, or press Enter to play again.')
    response = input('> ').upper()
    if response == 'QUIT':
        print('Thanks for playing!')
        sys.exit() 

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果把第 22 行的drawTime = time.time()改成drawTime = 0会怎么样?
  2. 如果把第 30 行的timeElapsed > 0.3改成timeElapsed < 0.3会怎么样?
  3. 如果把第 24 行的time.time() - drawTime改成time.time() + drawTime会怎么样?
  4. 如果删除或注释掉第 15 行的input('Press Enter to begin...')会发生什么?

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/412037.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【跟着陈七一起学C语言】今天总结:C语言的函数相关知识

友情链接&#xff1a;专栏地址 知识总结顺序参考C Primer Plus&#xff08;第六版&#xff09;和谭浩强老师的C程序设计&#xff08;第五版&#xff09;等&#xff0c;内容以书中为标准&#xff0c;同时参考其它各类书籍以及优质文章&#xff0c;以至减少知识点上的错误&#x…

太阳能电池板AI视觉检测:不良品全程阻断,高效助力光伏扩产

2022年&#xff0c;面对复杂严峻的国内外形势&#xff0c;我国光伏行业依然实现高速增长&#xff0c;多晶硅、硅片、电池片、组件产量稳居全球首位。2023年以来&#xff0c;扩产项目已多点开花。光伏装机量天花板将不断提升&#xff0c;分布式电站占比也将逐年上升。中国光伏行…

4月软件测试面试太难,吃透这份软件测试面试笔记后,成功跳槽涨薪30K

4 月开始&#xff0c;生活工作渐渐步入正轨&#xff0c;但金三银四却没有往年顺利。昨天跟一位高级架构师的前辈聊天时&#xff0c;聊到今年的面试。有两个感受&#xff0c;一个是今年面邀的次数比往年要低不少&#xff0c;再一个就是很多面试者准备明显不足。不少候选人能力其…

python学籍管理系统

1&#xff0c;创建登陆的首页面&#xff0c;且封装起来。LoginPage.py import tkinter as tk#导入tk模块 from tkinter import messagebox#导入消息提示模块 from tkinter import messagebox from db import db #导入数据库db class LoginPage:#把整个登陆页面创建一个class类…

搭建自己的饥荒Don‘t Starve服务器-饥荒Don‘t Starve开服教程

前言 饥荒这个游戏&#xff0c;虽然首发于2016年&#xff0c;但是贵在好玩呀。和Minecraft一样&#xff0c;可玩性很高&#xff0c;并且有很多mods&#xff0c;最近和小伙伴玩的过程中&#xff0c;就想着搭建一个服务器&#xff0c;方便在主机玩家不在线时候&#xff0c;也可以…

Linux软件安装---Tomcat安装

安装Tomcat 操作步骤&#xff1a; 使用xftp上传工具将tomcat的 二进制发布包上传到Linux解压安装包&#xff0c;命令为tar -zxvf apache-tomcat*** -C /usr/local进入Tomcat的bin的启动目录&#xff0c;命令为sh startup.sh或者./startup.sh 验证Tomcat启动是否成功&#xff0…

LeetCode:376. 摆动序列——说什么贪心和动规~

&#x1f34e;道阻且长&#xff0c;行则将至。&#x1f353; &#x1f33b;算法&#xff0c;不如说它是一种思考方式&#x1f340;算法专栏&#xff1a; &#x1f449;&#x1f3fb;123 一、&#x1f331;376. 摆动序列 题目描述&#xff1a;如果连续数字之间的差严格地在正数和…

Python 小型项目大全 46~50

# 四十六、百万骰子投掷统计模拟器 原文&#xff1a;http://inventwithpython.com/bigbookpython/project46.html 当你掷出两个六面骰子时&#xff0c;有 17%的机会掷出 7。这比掷出 2 的几率好得多&#xff1a;只有 3%。这是因为只有一种掷骰子的组合给你 2&#xff08;当两个…

「 分布式技术 」一致性哈希算法(Hash)详解

「 分布式技术 」一致性哈希算法&#xff08;Hash&#xff09;详解 参考&鸣谢 一致性 Hash 算法原理总结 kylinkzhang&#xff0c;腾讯 CSIG 后台开发工程师 什么是一致性哈希&#xff1f; xiaolinCoding 文章目录「 分布式技术 」一致性哈希算法&#xff08;Hash&#xff…

imagenet val 按类别分类

前言 有时候想看imagenet下某个类别的效果&#xff0c;但它又没划分… 之前看了这篇文章将ImageNet的验证集val数据分类到不同文件夹中&#xff0c;但不是很清楚那代码。 本文基于它的代码去做更改 把这个下下来 https://raw.githubusercontent.com/soumith/imagenetloader.…

ChatGPT 有哪些神奇的使用方式?

在遇到 ChatGPT之前&#xff0c;我很难想象&#xff0c;仅仅不到30s就能做出一个PPT。 而且对于小白来说&#xff0c;这个PPT绝对是「有水准、能拿得出手」的那种。 下面就是我用ChatGPTMindShow做的一套以分享短视频玩法为主题的 PPT&#xff0c;我挑几页大家看一下。 上面这…

10.Java面向对象----继承

Java面向对象—继承 面向对象简称 OO&#xff08;Object Oriented&#xff09;&#xff0c;20 世纪 80 年代以后&#xff0c;有了面向对象分析&#xff08;OOA&#xff09;、 面向对象设计&#xff08;OOD&#xff09;、面向对象程序设计&#xff08;OOP&#xff09;等新的系统…

2023 Java 面试题之MyBatis篇

持续更新内容涵盖&#xff1a;Java、MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、Redis、MySQL、Spring、Spring Boot、Spring Cloud、RabbitMQ、Kafka、 Linux 等技术栈&#xff08;滴滴滴.会持续更新哦&#xff0c;记得点赞、关注、分享三连击哈&#xff09;. My…

NumPy 秘籍中文第二版:十、Scikits 的乐趣

原文&#xff1a;NumPy Cookbook - Second Edition 协议&#xff1a;CC BY-NC-SA 4.0 译者&#xff1a;飞龙 在本章中&#xff0c;我们将介绍以下秘籍&#xff1a; 安装 scikit-learn加载示例数据集用 scikit-learn 对道琼斯股票进行聚类安装 Statsmodels使用 Statsmodels 执行…

【linux】——引导过程与服务控制

文章目录1.linux操作系统引导过程1.1 引导过程总览1.2 linux操作系统的引导过程1.3 系统初始化进程1.4 Systemd单元类型1.5 运行级别所对应的systemd目标2.排除启动类故障2.1 修复MBR扇区故障2.2 实例&#xff1a;修复MBR扇区故障2.2 修复GRUB引导故障2.3 实例&#xff1a;恢复…

电子数据取证(一)

电子数据取证概述 一&#xff0c;什么是电子数据 电子数据的特点 **1、以数字化形式存在。**所有的电子数据都是基于计算机应用和通信等电子化技术手段形成的&#xff0c;用以表示文字、图形符号、数字、字母等信息的资料。与其他证据种类不同&#xff0c;电子数据在本质上而…

Perpetuumsoft OLAP ModelKit .NET CRACK

关于 OLAP ModelKit 专业版 可视化您的数据透视表数据。OLAP ModelKit 是用 C# 编写的 .NET 多功能 OLAP 组件&#xff0c;仅包含 100% 托管代码。它具有 XP 主题外观和使用任何 .NET 数据源&#xff08;ADO.NET 和 IList&#xff09;的能力。通过在任何第三方组件&#xff08;…

java 面试消息题1-13

1. Redis 线程模型&#xff0c; 及为什么redis 这么快&#xff1f; 1.Redis虽然是一条一条处理命令的&#xff08;单线程&#xff09;&#xff0c;但是redis把每一条命令分成了很多个小命令&#xff0c;对这些小命令是多线程执行的。 2. IO 多路复用 - 可以用别人用过的IO。 …

RK3568平台开发系列讲解(调试篇)Oops 日志分析

🚀返回专栏总目录 文章目录 一、OOPS 日志分析二、OOPS 上的跟踪转储三、使用 objdump 识别内核模块中的错误代码行沉淀、分享、成长,让自己和他人都能有所收获!😄 📢编写代码并不总是内核开发中最难的方面。 调试是真正的瓶颈,即使对于经验丰富的内核开发人员也是如此…

Java同学入职环境安装全讲解

一、简述 最近入职一家新公司&#xff0c;拿到新电脑&#xff0c;那肯定有绕不开的装开发环境流程。下面我就从安装jdk、maven、git、idea四个方面讲解&#xff08;主要提供各个软件官方的下载网址&#xff0c;因为百度搜出来的东西大家懂的都懂我就不多说了&#xff09;。如果…