IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python语言程序设计Y.Daniel Liang练习题Chapter5 -> 正文阅读

[Python知识库]Python语言程序设计Y.Daniel Liang练习题Chapter5

ckp系列

ckp5.3

'''

i = 1
while i < 10:
    if i % 2 ==0:
        print(i)
# 无穷次

i = 1
while i < 10:
    if i % 2 == 0:
        print(i)
        i += 1
# 无穷次
'''
i = 1
while i < 10:
    if i % 2 == 0:
        print(i)
    i += 1
# 9次

ckp5.4

'''
count = 0
while count < 100:
    print(count)


count = 0
while count < 100:
    print(count)
    count -= 1


count = 0
while count < 100:
count += 1
'''

?

?

?

?

ckp5.5

number = eval(input("Enter an integer: "))
max = number

while number != 0:
    number = eval(input("Enter an integer: "))
    if number > max:
        max = number

print("max is ", max)

ckp5.6

number = 0
sum = 0

for count in range(5):
    number = eval(input("Enter an integer: "))
    sum += number

print("sum is ", sum)
print("count is ", count)

?

?

ckp5.7

'''
yes! 每一个 while 循环都可以用 for 循环来表示。
for 循环相对于 while 循环而言更加直观,他有一个range(),所以我们知道他循环的范围。
'''

ckp5.8

i = 0
sum = 0
while i < 1002:
    sum += i
    i += 1
print(sum)

?

?

ckp5.9

sum = 0
for i in range(1, 10000):
    sum = sum + i
    if sum > 10000:
        sum = sum - i
        break
print(sum)
print(i)

?

?

ckp5.10

'''
count = 0
while count < n:
    count += 1
# n+1 次

for count in range(n):
    print(count)
# n 次

count = 5
while count < n:
    count += 1
# n-5 次

count = 5
while count < n
    count = count + 3
# round((n-5)/3+1次
'''

ckp5.12

# Prompt the user to enter two integers
n1 = eval(input("Enter first integer: "))
n2 = eval(input("Enter second integer: "))

gcd = 1
k = 2

while k <= round(n1 / 2) and k <= round(n2 / 2):
    if n1 % k == 0 and n2 % k == 0:
        gcd = k
    k += 1

print("The greatest common divisor for",
      n1, "and", n2, "is", gcd)

?

?

ckp5.13

'''
break用于跳出一个循环体或者完全结束一个循环,不仅可以结束其所在的循环,还可结束其外层循环。
continue语句的作用是跳过本次循环体中剩下尚未执行的语句,立即进行下一次的循环条件判定,
可以理解为只是中止(跳过)本次循环,接着开始下一次循环。
'''
balance = 1000
while True:
    if balance < 9:
        break
    balance = balance - 9

print("Balance is ", balance) # output = 1


balance = 1000
while True:
    if balance < 9:
        continue
    balance = balance - 9

print("Balance is ", balance) #不会终止

?

?

ckp5.14

sum = 0
i = 0
while i < 3:
    i += 1
    if i % 3 == 0:
        continue
    sum += i
print(sum)
'''
sum = 0
for i in range(4):
    if i % 3 == 0:
        continue
    sum += i
print(sum)
'''

?

?

ckp5.15

#listings 5.11
sum = 0
number = 0
i = 0
while i < 20:
    number += 1
    i += 1
    sum += number
    if sum >= 100:
        i = 20
print("The number is", number)
print("The sum is", sum)

#listings 5.12
sum = 0
number = 0
while number < 20:
    number += 1
    sum += number

sum = sum - 10 - 11
print("The sum is", sum)

?

?

?

ckp5.16

for i in range(1, 4):
    for j in range(1, 4):
        if i * j > 2:
            break
        print(i * j)
    print(i)

print()

for i in range(1, 4):
    for j in range(1, 4):
        if i * j > 2:
            continue
        print(i * j)
    print(i)


?

list系列

list5.2

import random

# Generate a random number to be guessed
number = random.randint(0, 100)

print("Guess a magic number between 0 and 100")

# Prompt the user to guess the number
guess = eval(input("Enter your guess: "))

if guess == number:
    print("Yes, the number is", number)
elif guess > number:
    print("Your guess is too high")
else:
    print("Your guess is too low")

?

?

list5.4

import random
import time

correctCount = 0# Count the number of correct answers
count = 0# Count the number of questions
NUMBER_OF_QUESTIONS = 5# Constant

startTime = time.time() # Get start time

while count < NUMBER_OF_QUESTIONS:
    # Generate two random single-digit integers
    number1 = random.randint(0, 9)
    number2 = random.randint(0, 9)
    # If number1 < number2, swap number1 with number2
    if number1 < number2:
        number1, number2 = number2, number1



    answer = eval(input("What is " + str(number1) + " - " +
                        str(number2) + "? "))


    if number1 - number2 == answer:
        print("You are correct!")
        correctCount +=1
    else:
        print("Your answer is wrong.\n", number1, "-",
              number2, "is", number1 - number2)


    count += 1


endTime = time.time() # Get end time
testTime = int(endTime - startTime) # Get test time
print("Correct count is", correctCount, "out of",
      NUMBER_OF_QUESTIONS, "\nTest time is", testTime, "seconds")

?

?

list5.6

print("          Multiplication Table")
# Display the number title
print(" |", end = '')
for j in range(1, 10):
    print(" ", j, end = '')
print() # Jump to the new line
print("——————————————————————————————————————————")

# Display table body
for i in range(1, 10):
    print(i, "|", end = '')
    for j in range(1, 10):
        print(format(i * j, "4d"), end = '')
    print() # Jump to the new line

?

?

list5.14

import turtle
from random import randint

turtle.speed(1) # Set turtle speed to slowest

# Draw 16-by-16 lattice
turtle.color("gray") # Color for lattice
x = -80
for y in range(-80, 80 + 1, 10):
    turtle.penup()
    turtle.goto(x, y) # Draw a horizontal line
    turtle.pendown()
    turtle.forward(160)

y = 80
turtle.right(90)
for x in range(-80, 80 + 1, 10):
    turtle.penup()
    turtle.goto(x, y) # Draw a vertical line
    turtle.pendown()
    turtle.forward(160)

turtle.pensize(3)
turtle.color("red")

turtle.penup()
turtle.goto(0, 0) # Go to the center
turtle.pendown()

x = y = 0 # Current pen location at the center of lattice
while abs(x) < 80 and abs(y) < 80:
    r = randint(0, 3)
    if r ==0:
        x += 10 # Walk right
        turtle.setheading(0)
        turtle.forward(10)
    elif r == 1:
        y -= 10 # Walk down
        turtle.setheading(270)
        turtle.forward(10)
    elif r == 2:
        x -= 10 # Walk left
        turtle.setheading(180)
        turtle.forward(10)
    elif r == 3:
        y += 10 # Walk up
        turtle.setheading(90)
        turtle.forward(10)

turtle.done()

?

?


sec系列

sec5.3

print("Kilograms   " + "   Pounds")
for i in range(1, 200, 2):
    j = 2.2 * i
    #print(format(j, ".1f").rjust(20))
    print(str(i).ljust(5), format(j, ".1f").rjust(15))
    '''
    ljust and rjust use in str, ljust左对齐,rjust右对齐
    '''

??

?

sec5.5

print("Kilograms\t" + "Pounds\t" + "|\t" + "Pounds\t" + "Kilogorams\t")
p = 0
for i in range(1, 200, 2):
    j = 2.2 * i
    # 给第二组数据准备
    p += 1
    m = 15 + p * 5
    n = m * 0.45

    print(str(i).ljust(5), format(j, ".1f").rjust(15),
          "\t", m, "\t", format(n, ".2f").rjust(8))

?

?

sec5.16

# Prompt the user to enter two integers
n1 = eval(input("Enter first integer: "))
n2 = eval(input("Enter second integer: "))

d = min(n1, n2)
gcd = d

for i in range(d + 1, 1, -1):
    if n1 % i == 0 and n2 % i == 0:
        gcd = i
        break

print("The greatest common divisor for",
      n1, "and", n2, "is", gcd)
'''
while k <= n1 and k <= n2:
    if n1 % k == 0 and n2 % k == 0:
        gcd = k
    k += 1
'''

?

?

sec5.19

这个金字塔我想了很多的办法,什么居中显示,靠右靠左都试过(ljust,rjust,format等),靠右的时候在10以内显示都很成果,但是10往后就出来金字塔了,歪歪扭扭的,我不满意。于是我想了一种很笨的办法,用 \t 的方式来表示,虽然很笨,但是效果却很好,如果你们有很好的方法请务必告诉我,在此多蟹!!!

附上邮箱:jhtfine@qq.com

n = eval(input("Enter the number of lines: "))
for i in range(1, n + 1):
    a = "\t"
    for k in range(i, n + 1):
        a = "\t" + a
    for j in range(1, i + 1):
        a += (str(i - j + 1) + "\t") # 从大到小
    for j in range(2, i + 1):
        a += (str(j) + "\t") # 从小到大
    print(a)

?

?

sec5.20

print("Pattern A")
for i in range(7):
    a = "\t"
    for j in range(1, i + 1):
        a = a + str(j) + "\t"
    print(a)

print("Pattern B")
for i in range(6, 0, -1):
    a = "\t"
    for j in range(1, i + 1):
        a = a + str(j) + "\t"
    print(a)

print("Pattern C")
for i in range(7):
    a = "\t"
    for k in range(i, 6):
        a = a + "\t"
    for j in range(i, 0, -1):
        a = a + str(j) + "\t"
    print(a)

print("Pattern D")
for i in range(6, 0, -1):
    a = "\t"
    for k in range(i, 6):
        a = a + "\t"
    for j in range(1, i + 1):
        a = a + str(j) + "\t"
    print(a)

?

?

?

sec5.47

import turtle
import random

turtle.pensize(3)
turtle.hideturtle()

turtle.penup()
turtle.goto(-60, 50)
turtle.pendown()
turtle.goto(60, 50)
turtle.goto(60, -50)
turtle.goto(-60, -50)
turtle.goto(-60, 50)
#turtle.done()

turtle.pensize(5)
turtle.pencolor("red")
for i in range(11):
    turtle.penup()
    x = random.randint(-60, 60)
    y = random.randint(-50, 50)
    turtle.goto(x, y)
    turtle.pendown()
    turtle.goto(x, y)
turtle.done()

?

?

sec5.55

这道棋盘题也稍有难度,我用的方法也很笨,用了两个很长的 for 语句,我总觉得有更好的办法,但是碍于我能力和时间有限,只能做到这种程度了,望见谅!!!

import turtle
from random import randint

turtle.speed(20)
turtle.color("gray")
x = -80
for y in range(-80, 80 + 1, 20):
    turtle.penup()
    turtle.goto(x, y) # Draw a horizontal line
    turtle.pendown()
    turtle.forward(160)

y = 80
turtle.right(90)
for x in range(-80, 80 + 1, 20):
    turtle.penup()
    turtle.goto(x, y) # Draw a vertical line
    turtle.pendown()
    turtle.forward(160)

for x in range(-80, 40 + 1, 40):
    for y in range(-60, 60 + 1, 40):
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        turtle.begin_fill()
        turtle.color("black")
        turtle.forward(20)
        turtle.left(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.end_fill()

for x in range(-60, 60 + 1, 40):
    for y in range(-40, 80 + 1, 40):
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        turtle.begin_fill()
        turtle.color("black")
        turtle.forward(20)
        turtle.left(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.end_fill()



turtle.hideturtle()
turtle.done()

?


?

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-22 10:53:42  更:2021-10-22 10:55:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 21:41:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码