Python 小型项目大全 11~15

十一、标题党生成器

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

我们的网站需要欺骗人们去看广告!但是想出有创意的原创内容太难了。幸运的是,有了标题党生成器,我们可以让一台计算机产生数百万个令人发指的虚假标题。都是低质量的,但读者似乎并不介意。这个程序根据你的需要从一个 MadLibs 风格的模板中生成尽可能多的标题。

这个程序中有很多标题模板的文本,但是代码本身很简单,适合初学者。

运行示例

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

Clickbait Headline Generator
By Al Sweigart email@protected

Our website needs to trick people into looking at ads!
Enter the number of clickbait headlines to generate:
> 1000
Big Companies Hate Him! See How This New York Cat Invented a Cheaper Robot
What Telephone Psychics Don't Want You To Know About Avocados
You Won't Believe What This North Carolina Shovel Found in Her Workplace
`--snip--`
14 Reasons Why Parents Are More Interesting Than You Think (Number 1 Will Surprise You!)
What Robots Don't Want You To Know About Cats
This Florida Telephone Psychic Didn't Think Robots Would Take Her Job. She Was Wrong.

工作原理

这个程序有几个函数来生成不同类型的标题党。他们每个人都从STATESNOUNSPLACESWHEN和其他列表中获得随机单词。这些函数然后用format()字符串方法将这些单词插入到一个模板字符串中,然后返回这个字符串。这就像一本“Mad Libs”活动书,只是电脑会填空,让程序在几秒钟内生成数千个标题党。

"""Clickbait Headline Generator, by Al Sweigart email@protected
A clickbait headline generator for your soulless content farm website.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: large, beginner, humor, word"""

import random

# Set up the constants:
OBJECT_PRONOUNS = ['Her', 'Him', 'Them']
POSSESIVE_PRONOUNS = ['Her', 'His', 'Their']
PERSONAL_PRONOUNS = ['She', 'He', 'They']
STATES = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania',
         'Illinois', 'Ohio', 'Georgia', 'North Carolina', 'Michigan']
NOUNS = ['Athlete', 'Clown', 'Shovel', 'Paleo Diet', 'Doctor', 'Parent',
        'Cat', 'Dog', 'Chicken', 'Robot', 'Video Game', 'Avocado',
        'Plastic Straw','Serial Killer', 'Telephone Psychic']
PLACES = ['House', 'Attic', 'Bank Deposit Box', 'School', 'Basement',
         'Workplace', 'Donut Shop', 'Apocalypse Bunker']
WHEN = ['Soon', 'This Year', 'Later Today', 'RIGHT NOW', 'Next Week']


def main():
   print('Clickbait Headline Generator')
   print('By Al Sweigart email@protected')
   print()

   print('Our website needs to trick people into looking at ads!')
   while True:
       print('Enter the number of clickbait headlines to generate:')
       response = input('> ')
       if not response.isdecimal():
           print('Please enter a number.')
       else:
           numberOfHeadlines = int(response)
           break  # Exit the loop once a valid number is entered.

   for i in range(numberOfHeadlines):
       clickbaitType = random.randint(1, 8)

       if clickbaitType == 1:
           headline = generateAreMillenialsKillingHeadline()
       elif clickbaitType == 2:
           headline = generateWhatYouDontKnowHeadline()
       elif clickbaitType == 3:
           headline = generateBigCompaniesHateHerHeadline()
       elif clickbaitType == 4:
           headline = generateYouWontBelieveHeadline()
       elif clickbaitType == 5:
           headline = generateDontWantYouToKnowHeadline()
       elif clickbaitType == 6:
           headline = generateGiftIdeaHeadline()
       elif clickbaitType == 7:
           headline = generateReasonsWhyHeadline()
       elif clickbaitType == 8:
           headline = generateJobAutomatedHeadline()

       print(headline)
   print()

   website = random.choice(['wobsite', 'blag', 'Facebuuk', 'Googles',
                            'Facesbook', 'Tweedie', 'Pastagram'])
   when = random.choice(WHEN).lower()
   print('Post these to our', website, when, 'or you\'re fired!')


# Each of these functions returns a different type of headline:
def generateAreMillenialsKillingHeadline():
   noun = random.choice(NOUNS)
   return 'Are Millenials Killing the {} Industry?'.format(noun)


def generateWhatYouDontKnowHeadline():
   noun = random.choice(NOUNS)
   pluralNoun = random.choice(NOUNS) + 's'
   when = random.choice(WHEN)
   return 'Without This {}, {} Could Kill You {}'.format(noun, pluralNoun, when)


def generateBigCompaniesHateHerHeadline():
   pronoun = random.choice(OBJECT_PRONOUNS)
   state = random.choice(STATES)
   noun1 = random.choice(NOUNS)
   noun2 = random.choice(NOUNS)
   return 'Big Companies Hate {}! See How This {}  {} Invented a Cheaper {}'.format(pronoun, state, noun1, noun2)


def generateYouWontBelieveHeadline():
   state = random.choice(STATES)
   noun = random.choice(NOUNS)
   pronoun = random.choice(POSSESIVE_PRONOUNS)
   place = random.choice(PLACES)
   return 'You Won\'t Believe What This {}  {} Found in {}  {}'.format(state, noun, pronoun, place)


def generateDontWantYouToKnowHeadline():
   pluralNoun1 = random.choice(NOUNS) + 's'
   pluralNoun2 = random.choice(NOUNS) + 's'
   return 'What {} Don\'t Want You To Know About {}'.format(pluralNoun1, pluralNoun2)


def generateGiftIdeaHeadline():
    number = random.randint(7, 15)
    noun = random.choice(NOUNS)
    state = random.choice(STATES)
    return '{} Gift Ideas to Give Your {} From {}'.format(number, noun, state)


def generateReasonsWhyHeadline():
    number1 = random.randint(3, 19)
    pluralNoun = random.choice(NOUNS) + 's'
    # number2 should be no larger than number1:
    number2 = random.randint(1, number1)
    return '{} Reasons Why {} Are More Interesting Than You Think (Number {} Will Surprise You!)'.format(number1, pluralNoun, number2)


def generateJobAutomatedHeadline():
    state = random.choice(STATES)
    noun = random.choice(NOUNS)

    i = random.randint(0, 2)
    pronoun1 = POSSESIVE_PRONOUNS[i]
    pronoun2 = PERSONAL_PRONOUNS[i]
    if pronoun1 == 'Their':
        return 'This {}  {} Didn\'t Think Robots Would Take {} Job. {} Were Wrong.'.format(state, noun, pronoun1, pronoun2)
    else:
        return 'This {}  {} Didn\'t Think Robots Would Take {} Job. {} Was Wrong.'.format(state, noun, pronoun1, pronoun2)


# If the program is run (instead of imported), run the game:
if __name__ == '__main__':
    main() 

在输入源代码并运行几次之后,尝试对其进行实验性的修改。你也可以自己想办法做到以下几点:

  • 添加其他类型的标题党。
  • 添加新的单词类别,超出NOUNSSTATES等。

探索程序

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

  1. 如果删除或注释掉第 34 行的numberOfHeadlines = int(response),会得到什么错误信息?
  2. 如果将第 34 行的int(response)改为response,会得到什么错误信息?
  3. 如果将第 19 行改为WHEN = [],会得到什么错误信息?

十二、柯拉茨序列

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

柯拉茨序列,也称为3n + 1问题,是最简单的不可能数学问题。(不过不用担心,程序本身对初学者来说已经足够简单了。)从一个起始数字,n,遵循三个规则得到序列中的下一个数字:

  1. 如果n是偶数,那么下一个数字n就是n / 2
  2. 如果n是奇数,那么下一个数n就是n * 3 + 1
  3. 如果n为 1,则停止。否则,重复。

一般认为,但迄今为止没有数学证明,每个起始数最终终止于 1。关于柯拉茨序列的更多信息可以在en.wikipedia.org/wiki/Collatz_conjecture找到。

运行示例

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

Collatz Sequence, or, the 3n + 1 Problem
By Al Sweigart email@protected

The Collatz sequence is a sequence of numbers produced from a starting
number n, following three rules:
`--snip--`
Enter a starting number (greater than 0) or QUIT:
> 26
26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

Collatz Sequence, or, the 3n + 1 Problem
By Al Sweigart email@protected
`--snip--`
Enter a starting number (greater than 0) or QUIT:
> 27
27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1

工作原理

模数运算符可以帮助您确定一个数字是偶数还是奇数。记住这个操作符是一种“余数”操作符。23 除以 7 是 3 余 2,而23 mod 7只是 2。偶数除以 2 没有余数,奇数除以 2 有余数 1。当n为偶数时,第 33 行if n % 2 == 0:中的条件求值为True。当n为奇数时,计算结果为False

"""Collatz Sequence, by Al Sweigart email@protected
Generates numbers for the Collatz sequence, given a starting number.
More info at: https://en.wikipedia.org/wiki/Collatz_conjecture
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, math"""

import sys, time

print('''Collatz Sequence, or, the 3n + 1 Problem
By Al Sweigart email@protected

The Collatz sequence is a sequence of numbers produced from a starting
number n, following three rules:

1) If n is even, the next number n is n / 2.
2) If n is odd, the next number n is n * 3 + 1.
3) If n is 1, stop. Otherwise, repeat.

It is generally thought, but so far not mathematically proven, that
every starting number eventually terminates at 1.
''')

print('Enter a starting number (greater than 0) or QUIT:')
response = input('> ')

if not response.isdecimal() or response == '0':
    print('You must enter an integer greater than 0.')
    sys.exit()

n = int(response)
print(n, end='', flush=True)
while n != 1:
    if n % 2 == 0:  # If n is even...
        n = n // 2
    else:  # Otherwise, n is odd...
        n = 3 * n + 1

    print(', ' + str(n), end='', flush=True)
    time.sleep(0.1)
print() 

探索程序

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

  1. 以 32 开头的柯拉茨序列中有多少个数字?
  2. 以 33 开头的柯拉茨序列中有多少个数字?
  3. 起始数为 2 的幂(2、4、8、16、32、64、128 等等)的排序序列是否总是只由偶数组成(除了最后的 1)?
  4. 输入0作为起始整数会发生什么?

#13 康威的生命游戏

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

康威的生命游戏是一种细胞自动机模拟,它遵循简单的规则来创建有趣的模式。它是由数学家约翰·康威在 1970 年发明的,并由马丁·加德纳在《科学美国人》的“数学游戏”专栏推广开来。今天,它是程序员和计算机科学家的最爱,尽管它更像是一个有趣的可视化而不是真正的“游戏”二维棋盘有一个“单元格”网格,每个单元格都遵循三个简单的规则:

  • 具有两个或三个邻居的活细胞在模拟的下一步中保持存活。
  • 在模拟的下一步中,正好有三个邻居的死细胞变成活的。
  • 在模拟的下一步中,任何其他细胞死亡或保持死亡。

下一步模拟中细胞的活或死状态完全取决于它们的当前状态。这些细胞不会“记住”任何旧的状态。关于这些简单规则产生的模式,有大量的研究。不幸的是,康威教授于 2020 年 4 月因并发症在新冠肺炎去世。更多关于康威《生命的游戏》的信息可以在en.wikipedia.org/wiki/Conway%27s_Game_of_Life找到,更多关于马丁·加德纳的信息可以在en.wikipedia.org/wiki/Martin_Gardner找到。

运行示例

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

 O                  O                 OO      O  O
O     O    O  O                 O                      O OOOO          O OO
OO    O   O                     O          O            O              O O
OO        O    O                          OO                        OO
OO        OO                             O O    O                    OO
                                         OO    O O                    O  OO
            OOO                          OO    OO                       O
                                                O    OOO
                                    O             O                     O O
                   OO             OO OO             OO  O
                   OOO               OO          OOOO    O  O
             O     OO                O O       O  OO  OO O   O    OO
             O  O                 O    O          O   OO O    O  OOO
             O                     OOOO  OO       OO   O    OOOOO O
OO            O                      O   OOO     O OOO        OOOO       O

工作原理

单元的状态存储在字典中的cellsnextCells变量中。两个字典都有键的(x, y)元组(其中xy是整数),活细胞的'O',死细胞的' '。第 40 到 44 行被设置为将这些字典的表示打印到屏幕上。cells变量的字典表示单元的当前状态,而nextCells存储模拟下一步中单元的字典。

"""Conway's Game of Life, by Al Sweigart email@protected
The classic cellular automata simulation. Press Ctrl-C to stop.
More info at: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: short, artistic, simulation"""

import copy, random, sys, time

# Set up the constants:
WIDTH = 79   # The width of the cell grid.
HEIGHT = 20  # The height of the cell grid.

# (!) Try changing ALIVE to '#' or another character:
ALIVE = 'O'  # The character representing a living cell.
# (!) Try changing DEAD to '.' or another character:
DEAD = ' '   # The character representing a dead cell.

# (!) Try changing ALIVE to '|' and DEAD to '-'.

# The cells and nextCells are dictionaries for the state of the game.
# Their keys are (x, y) tuples and their values are one of the ALIVE
# or DEAD values.
nextCells = {}
# Put random dead and alive cells into nextCells:
for x in range(WIDTH):  # Loop over every possible column.
    for y in range(HEIGHT):  # Loop over every possible row.
        # 50/50 chance for starting cells being alive or dead.
        if random.randint(0, 1) == 0:
            nextCells[(x, y)] = ALIVE  # Add a living cell.
        else:
            nextCells[(x, y)] = DEAD  # Add a dead cell.

while True:  # Main program loop.
    # Each iteration of this loop is a step of the simulation.

    print('\n' * 50)  # Separate each step with newlines.
    cells = copy.deepcopy(nextCells)

    # Print cells on the screen:
    for y in range(HEIGHT):
        for x in range(WIDTH):
            print(cells[(x, y)], end='')  # Print the # or space.
        print()  # Print a newline at the end of the row.
    print('Press Ctrl-C to quit.')

    # Calculate the next step's cells based on current step's cells:
    for x in range(WIDTH):
        for y in range(HEIGHT):
            # Get the neighboring coordinates of (x, y), even if they
            # wrap around the edge:
            left  = (x - 1) % WIDTH
            right = (x + 1) % WIDTH
            above = (y - 1) % HEIGHT
            below = (y + 1) % HEIGHT

            # Count the number of living neighbors:
            numNeighbors = 0
            if cells[(left, above)] == ALIVE:
                numNeighbors += 1  # Top-left neighbor is alive.
            if cells[(x, above)] == ALIVE:
                numNeighbors += 1  # Top neighbor is alive.
            if cells[(right, above)] == ALIVE:
                numNeighbors += 1  # Top-right neighbor is alive.
            if cells[(left, y)] == ALIVE:
                numNeighbors += 1  # Left neighbor is alive.
            if cells[(right, y)] == ALIVE:
                numNeighbors += 1  # Right neighbor is alive.
            if cells[(left, below)] == ALIVE:
                numNeighbors += 1  # Bottom-left neighbor is alive.
            if cells[(x, below)] == ALIVE:
                numNeighbors += 1  # Bottom neighbor is alive.
            if cells[(right, below)] == ALIVE:
                numNeighbors += 1  # Bottom-right neighbor is alive.

            # Set cell based on Conway's Game of Life rules:
            if cells[(x, y)] == ALIVE and (numNeighbors == 2
                or numNeighbors == 3):
                    # Living cells with 2 or 3 neighbors stay alive:
                    nextCells[(x, y)] = ALIVE
            elif cells[(x, y)] == DEAD and numNeighbors == 3:
                # Dead cells with 3 neighbors become alive:
                nextCells[(x, y)] = ALIVE
            else:
                # Everything else dies or stays dead:
                nextCells[(x, y)] = DEAD

    try:
        time.sleep(1)  # Add a 1 second pause to reduce flickering.
    except KeyboardInterrupt:
        print("Conway's Game of Life")
        print('By Al Sweigart email@protected')
        sys.exit()  # When Ctrl-C is pressed, end the program. 

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

  • 调整开始为活细胞的百分比,而不是总是使用 50%。
  • 添加从文本文件中读取初始状态的功能,这样用户可以手动编辑起始单元格状态。

探索程序

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

  1. 把 10 行的WIDTH = 79改成WIDTH = 7会怎么样?
  2. 如果删除或注释掉第 36 行的print('\n' * 50)会发生什么?
  3. 如果把第 28 行的random.randint(0, 1)改成random.randint(0, 10)会怎么样?
  4. 如果把第 85 行的nextCells[(x, y)] = DEAD改成nextCells[(x, y)] = ALIVE会怎么样?

十四、倒计时

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

这个程序显示一个数字计时器,倒计时到零。第六十四个项目的sevseg.py模块“七段显示模块”为每个数字生成图形,而不是直接呈现数字字符。您必须先创建这个文件,倒计时程序才能运行。然后,将倒计时设置为你喜欢的任何秒数、分钟数和小时数。这个程序类似于项目 19,“数字钟。”

运行示例

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

 __   __       __   __       __   __
|  | |  |  *  |  | |  |  *   __| |__|
|__| |__|  *  |__| |__|  *  |__   __|

Press Ctrl-C to quit.

工作原理

运行import sevseg之后,您可以调用sevseg.getSevSegStr()函数来获得一个包含七个段数字的多行字符串。然而,倒计时程序需要在时、分、秒之间显示一个由星号组成的冒号。这需要用splitlines()方法将这些数字的三行多行字符串分割成三个单独的字符串。

"""Countdown, by Al Sweigart email@protected
Show a countdown timer animation using a seven-segment display.
Press Ctrl-C to stop.
More info at https://en.wikipedia.org/wiki/Seven-segment_display
Requires sevseg.py to be in the same folder.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, artistic"""

import sys, time
import sevseg  # Imports our sevseg.py program.

# (!) Change this to any number of seconds:
secondsLeft = 30

try:
    while True:  # Main program loop.
        # Clear the screen by printing several newlines:
        print('\n' * 60)

        # Get the hours/minutes/seconds from secondsLeft:
        # For example: 7265 is 2 hours, 1 minute, 5 seconds.
        # So 7265 // 3600 is 2 hours:
        hours = str(secondsLeft // 3600)
        # And 7265 % 3600 is 65, and 65 // 60 is 1 minute:
        minutes = str((secondsLeft % 3600) // 60)
        # And 7265 % 60 is 5 seconds:
        seconds = str(secondsLeft % 60)

        # Get the digit strings from the sevseg module:
        hDigits = sevseg.getSevSegStr(hours, 2)
        hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines()

        mDigits = sevseg.getSevSegStr(minutes, 2)
        mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines()

        sDigits = sevseg.getSevSegStr(seconds, 2)
        sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines()

        # Display the digits:
        print(hTopRow    + '     ' + mTopRow    + '     ' + sTopRow)
        print(hMiddleRow + '  *  ' + mMiddleRow + '  *  ' + sMiddleRow)
        print(hBottomRow + '  *  ' + mBottomRow + '  *  ' + sBottomRow)

        if secondsLeft == 0:
            print()
            print('    * * * * BOOM * * * *')
            break

        print()
        print('Press Ctrl-C to quit.')

        time.sleep(1)  # Insert a one-second pause.
        secondsLeft -= 1
except KeyboardInterrupt:
    print('Countdown, by Al Sweigart email@protected')
    sys.exit()  # When Ctrl-C is pressed, end the program.) 

在输入源代码并运行几次之后,尝试对其进行实验性的修改。你也可以自己想办法做到以下几点:

  • 提示用户输入开始倒计时的时间。
  • 让用户输入在倒计时结束时显示的消息。

探索程序

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

  1. 如果把第 13 行的secondsLeft = 30改成secondsLeft = 30.5会怎么样?
  2. 如果把第 30、33、36 行的2改成1会怎么样?
  3. 如果把 52 行的time.sleep(1)改成time.sleep(0.1)会怎么样?
  4. 如果把 53 行的secondsLeft -= 1改成secondsLeft -= 2会怎么样?
  5. 如果删除或注释掉第 18 行的print('\n' * 60)会发生什么?
  6. 如果删除或注释掉第 10 行的import sevseg,会得到什么错误信息?

十五、深坑

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

这个节目是一个永远深入地下的深洞的动画。虽然很短,但这个程序利用了计算机屏幕的滚动特性,产生了一个有趣且无休止的可视化效果,证明了制作有趣的东西并不需要太多代码。这个项目类似于 58 项目“彩虹”

运行示例

当您运行deepcave.py时,输出如下:

Deep Cave, by Al Sweigart email@protected
Press Ctrl-C to stop.
####################          ########################################
####################         #########################################
####################          ########################################
####################          ########################################
#####################          #######################################
######################          ######################################
#####################          #######################################
####################          ########################################
###################          #########################################
`--snip--`

工作原理

这个程序利用了这样一个事实,即打印新行最终会导致前面的行在屏幕上上移。通过在每行上打印一个稍微不同的间隙,程序创建了一个滚动动画,看起来好像观众在向下移动。

左侧的井号字符数由leftWidth变量跟踪。中间的空格数由gapWidth变量跟踪。右侧标签字符的数量从WIDTH - gapWidth - leftWidth开始计算。这确保了每一行总是相同的宽度。

"""Deep Cave, by Al Sweigart email@protected
An animation of a deep cave that goes forever into the earth.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, scrolling, artistic"""


import random, sys, time

# Set up the constants:
WIDTH = 70  # (!) Try changing this to 10 or 30.
PAUSE_AMOUNT = 0.05  # (!) Try changing this to 0 or 1.0.

print('Deep Cave, by Al Sweigart email@protected')
print('Press Ctrl-C to stop.')
time.sleep(2)

leftWidth = 20
gapWidth = 10

while True:
    # Display the tunnel segment:
    rightWidth = WIDTH - gapWidth - leftWidth
    print(('#' * leftWidth) + (' ' * gapWidth) + ('#' * rightWidth))

    # Check for Ctrl-C press during the brief pause:
    try:
        time.sleep(PAUSE_AMOUNT)
    except KeyboardInterrupt:
        sys.exit()  # When Ctrl-C is pressed, end the program.

    # Adjust the left side width:
    diceRoll = random.randint(1, 6)
    if diceRoll == 1 and leftWidth > 1:
        leftWidth = leftWidth - 1  # Decrease left side width.
    elif diceRoll == 2 and leftWidth + gapWidth < WIDTH - 1:
        leftWidth = leftWidth + 1  # Increase left side width.
    else:
        pass  # Do nothing; no change in left side width.

    # Adjust the gap width:
    # (!) Try uncommenting out all of the following code:
    #diceRoll = random.randint(1, 6)
    #if diceRoll == 1 and gapWidth > 1:
    #    gapWidth = gapWidth - 1  # Decrease gap width.
    #elif diceRoll == 2 and leftWidth + gapWidth < WIDTH - 1:
    #    gapWidth = gapWidth + 1  # Increase gap width.
    #else:
    #    pass  # Do nothing; no change in gap width. 

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

探索程序

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

  1. 如果把第 23 行的(' ' * gapWidth)改成('.' * gapWidth)会怎么样?
  2. 如果把第 32 行的random.randint(1, 6)改成random.randint(1, 1)会怎么样?
  3. 如果把第 32 行的random.randint(1, 6)改成random.randint(2, 2)会怎么样?
  4. 如果删除或注释掉第 17 行的leftWidth = 20,会得到什么错误信息?
  5. 如果把第 10 行的WIDTH = 70改成WIDTH = -70会怎么样?
  6. 如果将第 11 行的PAUSE_AMOUNT = 0.05改为PAUSE_AMOUNT = -0.05,会得到什么错误信息?

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】浅析Input子系统

文章目录1. 框架1.1 数据结构1.2 evdev_handler1.3 evdev_init1.4 input_register_handler2. 应用如何打开节点并读取到事件数据2.1 evdev_fops2.2 evdev_open2.3 evdev_release2.4 evdev_read2.5 evdev_write2.6 evdev_poll2.7 evdev_fasync2.8 evdev_ioctl2.9 evdev_ioctl_co…

[考研数据结构]第3章之栈的基本知识与操作

文章目录 栈的基本概念 栈的实现 顺序栈 共享栈 链栈 栈的基本概念 栈的定义 栈&#xff08;Stack&#xff09;是只允许在一端进行插入或删除操作的线性表 相关术语 栈顶&#xff08;Top&#xff09;线性表允许进行插入或删除的那一端称之为栈顶栈底&#xff08;Bottom&…

【计算机网络-数据链路层】集线器、网桥、交换机

本文许多文字和图片使用了湖科大教书匠&#xff08;高军老师&#xff09;的 PPT&#xff0c;在此表示感谢。正是他让非科班的我能以奇妙的方式走进网络的世界。 文章目录1 【物理层】集线器&#xff08;Hub&#xff09;——共享式以太网1.1 为什么使用集线器&#xff1f;1.2 集…

macOS Monterey 12.6.5 (21G531) Boot ISO 原版可引导镜像

本站下载的 macOS 软件包&#xff0c;既可以拖拽到 Applications&#xff08;应用程序&#xff09;下直接安装&#xff0c;也可以制作启动 U 盘安装&#xff0c;或者在虚拟机中启动安装。另外也支持在 Windows 和 Linux 中创建可引导介质。 2023 年 4 月 10 日&#xff08;北京…

ESXI 6.7全面系统教程~汇总

ESXI 6.7全面系统教程 许可证&#xff1a;0A65P-00HD0-375M1-M097M-22P7H esxi 是一个脱机系统&#xff0c;也是一个虚拟机系统与vmware 相比&#xff0c;它可以直接运行在硬件上&#xff0c;这样可以减少资源浪费&#xff0c;一般用于服务器上&#xff1b;下面是esxi 的完整…

stable-diffusion-webui-colab部署记录

stable-diffusion-webui-colab 该模型可以在网上云端部署stable-diffusion&#xff0c;减少本地部署的繁琐步骤降低配置要求的依赖。 一、进入stable-diffusion-webui-colab 1.网址&#xff1a;https://github.com/camenduru/stable-diffusion-webui-colab 在分支中选择driv…

我的创作纪念日:Unity CEO表示生成式AI将是Unity近期发展重点,发布神秘影片预告

PICK 未来的AI技术将会让人类迎来下一个生产力变革&#xff0c;这其中也包括生成型AI的突破性革新。各大公司也正在竞相推出AIGC工具&#xff0c;其中微软的Copilot、Adobe的Firefly、Github的chatGPT等引起了人们的关注。然而&#xff0c;游戏开发领域似乎还没有一款真正针对性…

Vulnhub:Digitalworld.local (Development)靶机

kali&#xff1a;192.168.111.111 靶机&#xff1a;192.168.111.130 信息收集 端口扫描 nmap -A -v -sV -T5 -p- --scripthttp-enum 192.168.111.130 查看网站首页源码 访问development目录&#xff0c;提示存在一个流量包 查看流量包发现另一个网站路径&#xff1a;/devel…

java继承类怎么写

继承类是通过把父类的方法和属性继承到一个类中&#xff0c;而子类的方法和属性是子类自己定义的。 Java中有一个很重要的概念叫做继承&#xff0c;这也是 Java语言的精髓所在。Java语言提供了一种机制&#xff0c;叫做派生类。在 Java中&#xff0c;如果没有实现了某个派生类方…

python 调用c++

python中调用c&#xff0c;函数参数用 int类型&#xff0c;返回值为类型1,且返回值为 false。 注意&#xff1a;如果你使用了C中的 false&#xff0c;则返回的是-1。 在 Python中调用C时&#xff0c;你会得到一个名为 bool的类&#xff0c;其中包含了两个成员变量&#xff1a; …

多智能体深度强化学习在移动边缘计算的联合多通道访问和任务卸载中的应用

多智能体深度强化学习在移动边缘计算的联合多通道访问和任务卸载中的应用主要贡献与相关工作比较的贡献三、系统模型&#xff08;only 2 pages&#xff09;3.1 网络模型3.2 通信模型3.3 计算模型3.3.1 本地计算3.3.2 卸载计算四、预备知识&#xff08;only 1 page&#xff09;五…

SpringCloud-Gateway网关搭建整合nacos配置中心实现动态路由整合sentinel实现服务限流熔点降级

官方文档(更多配置详情直接查看官方文档) 为什么需要服务网关 传统的单体架构中只需要开放一个服务给客户端调用&#xff0c;但是微服务架构中是将一个系统拆分成多个微服务&#xff0c;如果没有网关&#xff0c;客户端只能在本地记录每个微服务的调用地址&#xff0c;当需要调…

安全防御 --- 恶意代码、防病毒

一、恶意代码 1、按照传播方式分类 &#xff08;1&#xff09;病毒 概念&#xff1a;病毒是一种基于硬件和操作系统的程序&#xff0c;具有感染和破坏能力&#xff0c;这与病毒程序的结构有关。病毒攻击的宿主程序是病毒的栖身地&#xff0c;它是病毒传播的目的地&#xff0…

MySQL库的操作

文章目录&#xff1a;创建数据库字符集和校验规则查看系统默认字符集和校验规则查看数据库支持的字符集查看数据库支持的字符集校验规则校验规则对数据库的影响操作数据库查看数据库显示创建语句修改数据库删除数据库数据库的备份和还原表的备份和还原查看连接情况创建数据库 …

数据库基础

文章目录前言一、什么是数据库二、主流数据库三、基本使用1.连接服务器2.服务器,数据库,表关系3.使用案例4.数据逻辑存储四、MySQL架构五、SQL分类六、存储引擎1.存储引擎2.查看存储引擎3.存储引擎对比总结前言 正文开始!!! 一、什么是数据库 存储数据用文件就可以了,为什么还…

【并发编程】AQS源码

ReentrantLock 互斥锁,可重入 AQS是可以支持互斥锁和共享锁的&#xff0c;这里只分析互斥锁的源码 加锁 公平锁和非公平锁 公平锁 final void lock() {acquire(1); //抢占1把锁.}// AQS里面的方法public final void acquire(int arg) { if (!tryAcquire(arg) &&acq…

IP协议(网络层重点协议)

目录 一、IP协议报头格式 二、地址选择 1、IP地址 &#xff08;1&#xff09;格式 &#xff08;2&#xff09;组成 &#xff08;3&#xff09;分类 &#xff08;4&#xff09;子网掩码 三、路由选择 IP协议是网络层的协议&#xff0c;它主要完成两个方面的任务&#xf…

redis基础(6.0)数据结构、事务、常用组件等

1 概述 1.1 redis介绍 Redis 是互联网技术领域使用最为广泛的存储中间件&#xff0c;它是「Remote Dictionary Service」的首字母缩写&#xff0c;也就是「远程字典服务」。Redis 以其超高的性能、完美的文档、 简洁易懂的源码和丰富的客户端库支持在开源中间件领域广受好评。…

车载网络 - Autosar网络管理 - 常用缩写

为了方便大家日常工作中的使用和交流&#xff0c;每块专业规范或者文章中&#xff0c;都会有或多或少的缩写使用&#xff0c;然而如果一段时间没使用&#xff0c;经常会忘记这些缩写到底代表的是什么意思&#xff0c;为了方便后续内容的介绍&#xff0c;也为了我自己后面忘记后…

做自动化测试时所谓的“难点”

这篇关于自动化测试的文章&#xff0c;可能和你看到的大多数自动化的文章有所不同。我不是一位专职的自动化测试工程师&#xff0c;没有开发过自动化的工具或者框架&#xff0c;用的自动化的工具也不多&#xff0c;也没有做过开发&#xff0c;所以我讲不出那些现在很多人很看重…
最新文章