Python 习题---列表字符串

列表和字符串

题1:创建一段文章中的单词列表,要求单词中无标点
import string
badChars = string.whitespace + string.punctuation  # 标点、字符等
def words(stence):
    words = []
    for word in stence.split(' '):
        for char in word:
            if char in badChars:
                word = word.replace(char,'')
        while word not in words and len(word)!=0:
            #word = word.strip(',.!?')  只适用于句首和句中
            words.append(word)
        else:
            continue
    return words

stence = input('Enter a stence:')
words(stence)
题2:turtle画美国国旗
# 画美国国旗
import turtle as t
def draw_strip(x,y,width,height,col):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.color(col,col)
    t.begin_fill()
    for i in range(4):
        if i%2 == 0:
            t.forward(width)
        else:
            t.forward(height)
        t.right(90)
    t.end_fill()

def draw_star(x, y, length, col):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.color(col, col)
    t.begin_fill()
    for i in range(5):
        t.forward(length)
        t.right(144)
    t.end_fill()

def draw_USA_stars(origin_x, origin_y, height, color):
    length = 0.05858 * height
    H, E = height * 0.0633, - height * 0.0538
    # draw outer stars
    pos_x, pos_y = origin_x  + H - length/2, origin_y + E + 0.009517 * height
    for row in range(5):
        for  col in range(6):
            draw_star(pos_x + col * H * 2, pos_y + row * E * 2, length, color)
    # draw inner stars
    pos_x, pos_y = origin_x - length/2 + H * 2, origin_y + E * 2 + 0.009517 * height
    for row in range(4):
        for col in range(5):
            draw_star(pos_x + col * H * 2, pos_y + row * E * 2, length, color)

def draw_USA_national_flag():
    t.speed(0)
    t.clear()
    origin_x, origin_y = -200, 200
    height = 130 * 2
    width = 13 * 19 * 2
    # draw strips
    for i in range(13):
        if i%2 == 0:
            color = 'red'
        else:
            color = 'white'
        draw_strip(origin_x, origin_y - height / 13 * i, width, height / 13, color)
    # draw blue rectangle
    draw_strip(origin_x, origin_y, width * 0.4, 7/13.0 * height, 'blue')
    # draw stars
    draw_USA_stars(origin_x, origin_y, height, 'white')
    t.penup()
    t.goto(-110,100)

draw_USA_national_flag()
题3:巴比伦平方根算法
# 巴比伦平方根算法

import math

numStr = input('Find the square root of integer:')
while not numStr.isdigit():
    print('Pay attention')
    numStr = input('Find the square root of integer:')
number = int(numStr)

guessStr = input('Initial guess:')
while not guessStr.isdigit():
    print('Pay attention')
    guessStr = input('Initial guess:')
guessInt = int(guessStr)

originalGuess_int = guessInt
tolerance_float = float(input('What tolerance:'))

previous = 0
count = 0

while math.fabs(previous - guessInt) > tolerance_float:
    previous = guessInt
    quotient = number/guessInt
    guessInt = (quotient+guessInt)/2
    count = count + 1

print('Square root of ',number,'is:',guessInt)
print('Took',count,'reps to get it to tolerance:',tolerance_float)
print('Starting from a guess of:',originalGuess_int)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值