Python一日一练02----诗词生成器

要求

编写一段程序,可以自动生成小诗。

格式如下

源码

import random
import sys

articles = ["the", "a", "another", "her", "his"]
subjects = ["cat", "dog", "horse", "man", "woman", "boy", "girl"]
verbs = ["sang", "ran", "jumped", "said", "fought", "swam", "saw",
         "heard", "felt", "slept", "hopped", "hoped", "cried",
         "laughed", "walked"]
adverbs = ["loudly", "quietly", "quickly", "slowly", "well", "badly",
           "rudely", "politely"]

lines = 5
if len(sys.argv) > 1:
    try:
        temp = int(sys.argv[1])
        if 1 <= temp <= 10:
            lines = temp
        else:
            print("lines must be 1-10 inclusive")
    except ValueError:
        print("usage: badpoetry.py [lines]")

while lines:
    article = random.choice(articles)
    subject = random.choice(subjects)
    verb = random.choice(verbs)
    if random.randint(0, 1) == 0:
        print(article, subject, verb)
    else:
        adverb = random.choice(adverbs)
        print(article, subject, verb, adverb)
    lines -= 1

如果有什么疑问欢迎到我的微信公众号提问~

时间: 2024-10-01 05:25:26

Python一日一练02----诗词生成器的相关文章

Python一日一练102----创建简易博客(下)

继续改博客 接着上一篇继续改咱的博客. 母语不是英语肿么办,博客上面英语好多啊! 更改很简单. 你可以做一个快速更改来观察Django管理工具是否被翻译成你想要的语言. 仅需在settings.py添加'django.middleware.locale.LocaleMiddleware' 到MIDDLEWARE_CLASSES设置中,并确保它在'django.contrib.sessions.middleware.SessionMiddleware'之后就可以了. 建立博客的前台部分 创建模版

Python 一日一练: 01

将数字转换为LCD模式 代码如下: 1 # -*- coding:utf-8 -*- 2 ''' 3 Python 小练习-1 : 将数字转换为LCD显示 4 ''' 5 lt = [' __ ', '|__ ', '|__|', ' |', ' __|', '| |', ' '] 6 dt = {0:[0, 5, 2],1:[6, 3, 3], 2:[0, 4, 1], 3:[0, 4, 4], 4:[6, 2, 3], 5:[0, 1, 4], 6:[0, 1, 2], 7:[0, 3, 3

Python一日一练05----怒刷点击量

功能 自己主动获取CSDN文章列表,并对每篇文章添加点击量. 源代码 import urllib.request import re import time import random from bs4 import BeautifulSoup p = re.compile('/a359680405/article/details/........') #自己的博客主页 url = "http://blog.csdn.net/a359680405" #使用build_opener()是

Python一日一练103----Django模版练习

要求 request.META 是一个Python字典,包含了所有本次HTTP请求的Header信息,比如用户IP地址和用户Agent.通过request.META.items()可得到这个字典.要求将META信息输出到网页. 输出结果 源码 index.html <html> <head><title>{{index_title}}</title></head> <body> {% block mainbody %} {%for k

Python一日一练01----字符输出

要求 编写一个程序,当输入python bigdigists.py 234234,会输出下列结果. 源码 import sys Zero = ["  ***  ",         " *   * ",         "*     *",         "*     *",         "*     *",         " *   * ",         "  *

Python一日一练06----怒刷点击量pyqt5版

功能 输入博主用户名,增加博主点击量 软件截图 部分源码 # coding=utf-8 __author__ = 'a359680405' from PyQt5 import QtWidgets from PyQt5.QtCore import QThread from PyQt5.QtCore import pyqtSignal from visitspage.UI.MainForm import Ui_MainForm from visitspage.UI.newForm import Ui

Python一日一练104----Django URLconf练习

要求 请实现当url是http://127.0.0.1:8000/sum/n/(n是数字)时,返回的页面显示1-n的累加和的结果. 源码 views.py文件 #coding:utf-8 from django.shortcuts import render from django.http import HttpResponse,Http404 # Create your views here. def num_plus(request,plus): try: plus=int(plus) e

Python一日一练03----输出网页

要求 将以下文档作为网页模版,编程实现由用户定义网页信息并将网页输出保存 <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" \ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/199

Python一日一练03----一元二次方程的计算

要求 由用户随意给定一元二次方程系数a.b.c,计算一元二次方程的解 源码 import cmath #包含复数运算 import math import sys def get_float(msg, allow_zero): x = None while x is None: try: x = float(input(msg)) if not allow_zero and abs(x) < sys.float_info.epsilon: #sys.float_info.epsilon是浮点数0