《python语言程序设计》第4章全部课后题解题步骤 2021-5月到2021-7第5次刷题。

这是我自学python 选择了2018年版的全国计算机等级考试二级教程 python语言设计教材里的第4章课后题。的做题结果和步骤。仅供参考。这是第5次做这一章,还有很多需要完善的地方。然后说明因为是第5次刷题,所以解题步骤里包含了一些该书第五章的方法。因此请大家不要误会

4.1


# a, b, c = eval(input("Enter a, b, c: "))
# divNumber = pow(b, 2)-(4*a*c)
# r1 = (-b + (divNumber ** 0.5))/(2*a)
# r2 = (-b - (divNumber ** 0.5))/(2*a)


# if divNumber > 0:
#     print(f"The roots are {r1} and {r2}")

# elif divNumber == 0:
#     print(f"The roots are {r1 or r2}")

# else:
#     print("The equation has no real roots")

4.2

import random

number1 = random.randint(0, 9)
number2 = random.randint(0, 9)
number3 = random.randint(0, 9)

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

print(number1, "+", number2,"+", number3, "=", answer,
      "is", number1 + number2 + number3 == answer)

4.3

a, b, c, d, e, f = eval(input("Enter a,b,c,d,e,f: "))
judgeNumber = (a * d) - (b * c)

# x = ((e * d) - (b * f)) / judgeNumber  #错误位置
# y = ((a * f) - (e * c)) / judgeNumber   #错误位置


if judgeNumber != 0:
    print(f"x is {x} and y is {y}")
    x = ((e * d) - (b * f)) / judgeNumber  #正确位置
    y = ((a * f) - (e * c)) / judgeNumber   #正确位置

else:
    print("The equation has no solution")

#2020-11做题思路不错 今天遇到的问题是ZeroDivisionError: division by zero。后来明白了x和y的运算公式应该放在if或者else里而不是放在外面

4.4


import random

number1 = random.randint(0, 99)
number2 = random.randint(0, 99)

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

print(number1, "+", number2,"=", answer,
      "is", number1 + number2 == answer)

4.5


tdNumber = eval(input("Enter today's day: "))
edNumber = eval(input("Enter the number of days elapsed since day: "))

toDay = (tdNumber + edNumber) % 7
# print(toDay)

if toDay == 0:
    days = "Sunday"

elif toDay == 1:
    days = "Monday"

elif toDay == 2:
    days = "Tuesday"

elif toDay == 3:
    days = "Wednesday"

elif toDay == 4:
    days = "Thursday"


elif toDay == 5:
    days = "Friday"

elif toDay == 6:
    days = "Saturday"


if tdNumber == 0: 
    curDay = "Sunday"
elif tdNumber == 1:
     curDay = "Monday"
elif tdNumber == 2:
     curDay = "Tuesday"
elif tdNumber == 3:
     curDay = "Wednesday"
elif tdNumber == 4:
     curDay = "Thursday"
elif tdNumber == 5:
     curDay = "Friday"
elif tdNumber == 6:
     curDay = "Saturday"

print(f"Today is {curDay} and the future day is {days}")

4.6


weight = eval(input("Enter weight in pounds: "))

heightFeet =  eval(input("Enter feet: ")) 
height = eval(input("Enter inches: "))

KILOGRAMS_PER_POUND = 0.45359237
METERS_PER_INCH = 0.0254 
feet = heightFeet * 12
weightInKilograms = weight * KILOGRAMS_PER_POUND
heightInMeters = (height + feet) * METERS_PER_INCH
bmi = weightInKilograms / pow(heightInMeters,2)

print("BMI is", format(bmi, ".2f"))
if bmi < 18.5:
    print("Underweight")

elif bmi < 25:
    print("Your are Normal")

elif bmi < 30:
    print("Overweight")
else:
    print("Obese")

4.7

amount = eval(input("Enter an amount, for example, 1156: "))
dollarsNumber = amount // 100
quartersNumber = amount % 100 // 25
dimesNumber = amount % 100 % 25 // 10
nickelsNumber = amount % 100 % 25 % 10 // 5
penniesNumber = amount % 100 % 25 % 10 % 5
if dollarsNumber <=1:
    print(f"dollar is {dollarsNumber}")
elif dollarsNumber >1:
    print(f"dollars is {dollarsNumber}")

print(f"quartersNumber is {quartersNumber}")
print(f"dimesNumber is {dimesNumber}")
print(f"nickelsNumber is {nickelsNumber}")
if penniesNumber <=1:
    print(f"penniesNumber is {penniesNumber}")
elif penniesNumber > 1:
    print(f"penniesNumbers is {penniesNumber}")

4.8

a, b, c = eval(input("Enter three integers: "))



if a < b < c:
    print(c, b, a)
elif b < a < c:
    print(c, a, b)
elif b < c < a:
    print(a, c, b)
elif c < b < a:
    print(a, b, c)
elif a < c < b:
    print(b, c, a)
elif c < a < b:
    print(b, a, c)
###本来用了max,min的方式找出最大和最小数,但是中间的数怎么找,没有办法。后来用了FOR循环。但是也不得章法
所以不得已用回if elif

4.9

weight1, price1 = eval(input("Enter weight and price for package1: "))
weight2, price2 = eval(input("Enter weight and price for package2: "))

packAge1 = price1 / weight1
packAge2 = price2 / weight2


if packAge2 > packAge1:
    print("Package 1 has the better price")
else:
    print("Package 2 has the better price")

4.10

import random

number1 = random.randint(0, 99)
number2 = random.randint(0, 99)

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

print(number1, "*", number2, "=", answer,
      "is", number1 * number2 == answer)

4.11 针对闰年要有一个判定

month = eval(input("Enter month is : "))
years = eval(input("Enter years is : "))


tYears = years % 4 == 0 and years % 100 != 0 or years % 400 == 0 #建立判定闰年条件

if month == 1:
    days = 31

elif tYears is True and month == 2: 比过去少了很多行代码。非常好,直接在这里简化
    days = 29
elif tYears is not True and month == 2:  第一次用is not 
    days = 28
elif month == 3:
    days = 31
elif month == 4:
    days = 30
elif month == 5:
    days = 31
elif month == 6:
    days = 30
elif month == 7:
    days = 31
elif month == 8:
    days = 31
elif month == 9:
    days = 30
elif month == 10:
    days = 31
elif month == 11:
    days = 30
elif month == 12:
    days = 31

print(f'year is {years} and month {month} is days number {days}')

4.12

a = eval(input("Enter an integer: "))

print(f"Is {a} divisible by 5 and 6 ?", a % 5 == 0 and a % 6 == 0)
print(f"Is {a} divisible by 5 or 6 ?", a % 5 == 0 or a % 6 == 0)
print(f"Is {a} divisible by 5 or 6, but not both ?", a % 5 == 0 or a % 6 == 0
      and not (a % 5 == 0 and 6 == 0))

4.13

import sys

status = eval(input(
    "(0-single filer, 1-married jointly, 2-married separately, 3-head of household)\n" +
    "Enter the filing status: "))

income = eval(input("Enter the taxable income: "))

tax = 0  #此处有while循环中 tax 的作用

if status == 0:
    if income <= 8350:
        tax = income * 0.10
    elif income <= 33950:
        tax = 8350 * 0.10 + (income - 8350) * 0.15
    elif income <= 82250:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25

    elif income <= 171550:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (
            income - 82250) * 0.28
    elif income <= 372950:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (
            171550 - 82250) * 0.28 + (income - 171550) * 0.33
    else:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (
            171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35
elif status == 1:
    if income <= 16700:
        tax = income * 0.10
    elif income <= 67900:
        tax = 16700 * 0.10 + (income - 16700) * 0.15
    elif income <= 137050:
        tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25

    elif income <= 208850:
        tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (
            income - 137050) * 0.28
    elif income <= 372950:
        tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (
            372950 - 137050) * 0.28 + (income - 208850) * 0.33

    else:
        tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (
            372950 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35

elif status == 2:
    if income <= 8350:
        tax = income * 0.10
    elif income <= 33950:
        tax = 8350 * 0.10 + (income - 8350) * 0.15
    elif income <= 68250:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25

    elif income <= 104425:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68250 - 33950) * 0.25 + (
            income - 68250) * 0.28
    elif income <= 186476:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68250 - 33950) * 0.25  + (
            104425 - 68250) * 0.28 + (income - 104425) * 0.33
    else:
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68250 - 33950) * 0.25  + (
            104425 - 68250) * 0.28 + (186475 - 104425) * 0.33 + (income - 186476) * 0.35

elif status == 3:
    if income <= 11950:
        tax = income * 0.10
    elif income <= 45500:
        tax = 11950 * 0.10 + (income - 11950) * 0.15
    elif income <= 117450:
        tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25

    elif income <= 190200:
        tax =  11950 * 0.10 + (45500 - 11950) * 0.15 +  (117450 - 45500) * 0.25 + (
            income - 117450) * 0.28
    elif income <= 372950:
        tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +  (117450 - 45500) * 0.25 + (
            190200 - 117450) * 0.28 + (income - 190200) * 0.33
    else:
        tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +  (117450 - 45500) * 0.25 + (
            190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35
else:
    print("Error: invalid staus")
    sys.exit()
print("Tax is", format(tax, ".2f"))

4.14

import random as r
guess = eval(input("Enter 0 and 1 for  like coin: "))
guessNumber = r.randint(0,1)
if guessNumber == 1:
      day = "正面"
elif guessNumber == 0:
      day = "反面"

if guessNumber == guess:
      print(f"guess is good {day}")
else:
      print("哥们你猜错了")

4.15

import random as r

lottery1 = r.randint(0, 9)
lottery2 = r.randint(0, 9)
lottery3 = r.randint(0, 9)
print(lottery1, lottery2, lottery3)
guess1, guess2, guess3 = eval(input("Enter your lottery pick(three digits): "))
a = (guess1,guess2,guess3)
b = (lottery1,lottery2,lottery3)

if a == b:
    print("Exact match: you win $10,000")
elif guess1 == lottery1 and guess2 == lottery3  and guess3 == lottery2:
      print("Exact math: you win $3000")
elif guess1 == lottery2 and guess2 == lottery3  and guess3 == lottery1:
      print("Exact math: you win $3000")
elif guess1 == lottery3 and guess2 == lottery2  and guess3 == lottery1:
      print("Exact math: you win $3000")
elif guess1 == lottery3 and guess2 == lottery1  and guess3 == lottery2:
      print("Exact math: you win $3000")
elif (guess1 == lottery1 or lottery3 or lottery2) and (guess2 != lottery1 or lottery2 or lottery3) and(guess3 != lottery1 or lottery2 or lottery3):
      print("Exact math: you win $1000")
elif (guess2 == lottery1 or lottery3 or lottery2) and (guess1 != lottery1 or lottery2 or lottery3) and(guess3 != lottery1 or lottery2 or lottery3):
      print("Exact math: you win $1000")
elif (guess3 == lottery1 or lottery3 or lottery2) and (guess1 != lottery1 or lottery2 or lottery3) and(guess2 != lottery1 or lottery2 or lottery3):
      print("Exact math: you win $1000")

4.16

import random as r 
a= r.randint(65,90)
print(chr(a))  ## chr是数字转ASCII码

4.17

import random as r

guessDigit = eval(input("scissor (0), rock(1).paper(2): "))

a = r.randint(0,2)

if guessDigit == 0:
      guessNumber = "scissor"
elif guessDigit == 1:
      guessNumber = "rock"
elif guessDigit == 2:
      guessNumber = "paper"

if a == 0:
   tNumber = "scissor"

elif a == 1:
   tNumber = "rock"
elif a == 2:
   tNumber = "paper"

if guessDigit == a:
      print(f"The computer is {tNumber}.You are {guessNumber}, So luck!")
elif guessDigit < a:
      print(f"The computer is {tNumber}.You are {guessNumber}, It is a draw!")
elif guessDigit > a:
      print(f"The computer is {tNumber}.You are {guessNumber}, You won!")

4.18

rate = eval(input("Enter the exchange rate from dollars to RMB: "))
conver = eval(input("Enter 0 to convert dollars to RMB and 1 vice versa: "))

if conver == 0:
      amount = eval(input("Enter the dollar amount: "))
      rmbDigit = rate * amount
      print(f"$ {amount} is {rmbDigit} yuan")

elif conver == 1:
      amount = eval(input("Enter the dollar amount: "))
      dollDigit = amount / rate
      print(f" {amount} yuan is ${dollDigit}")

else:
      print("Incorrect input")

4.19

x1, x2, x3 = eval(input("Enter three edges: "))

if x1 + x2 > x3 and x2 + x3 > x1 and x1+x3>x2:
      print(f"The perimeter is {x1+x2+x3}")
else:
      print("The input is invalid")

4.20

tempA = eval(input("Enter the temperature in Fahrenheit between -58 and 41: "))
if tempA > -58 and tempA < 41:
      v = eval(input("Enter the wind speed in miles per hour:"))
      tempWC = 35.74 + (0.6215 * tempA) - (35.75 * (v ** 0.16)) + \
             (0.4275 * tempA * (v ** 0.16))
      print(f"Temp is {tempWC}")
else:
    print("The input is invalid")

4.21

yearsDigit = eval(input("Enter year: (e.g.,: 2008): "))
monthsDigit = eval(input("Enter month 1-12: "))
daysDigit = eval(input("Enter the day of the month 1-31: "))

j = yearsDigit // 100
print(j)
k = yearsDigit % 100  # 此值属于不含1月2月的。所以1月2月需要单独
print(k)

if monthsDigit == 1 or monthsDigit == 2:
    monthsDigit += 12
    print(monthsDigit)
    yearsDigit -= 1
    k = yearsDigit % 100  # 此处需单独附一个值,如果没有此值。所有1月2月的都将出错。之前2020年版本忽略此问题。、
    print(yearsDigit)

m = 26 * (monthsDigit + 1)
h = (daysDigit + (m/10 // 1) + k + (k/4 // 1) + (j/4 // 1) + (5 * j)) % 7
print(f"h is {h}")

if h == 0:
    print("Day of the week is Saturday")
elif h == 1:
    print("Day of the week is Sunday")
elif h == 2:
    print("Day of the week is Monday")
elif h == 3:
    print("Day of the week is Tuesday")
elif h == 4:
    print("Day of the week is Wednesday")
elif h == 5:
    print("Day of the week is Thursday")
elif h == 6:
    print("Day of the week is Friday")

4.22

x1,y1,radius =eval(input("Enter x,y,raidus digit: "))

x0 = 0
y0 = 0

disDigit = (pow((x1-x0),2) + pow((y1-y0),2)) ** 0.5

if disDigit > radius:
      print(f"Point{x1,y1} is not in the circle")
else:
      print(f"Point{x1,y1} is in the circle")

4.23

x1, y1 = eval(input("Enter a point with two coordinates: "))
x0, y0 = 0, 0

width = 100 / 2
hight = 50 / 2

此段是我们测试用的方式。认为用距离来判断。但是此距离为点与点之间的直线距离。而判断一个点

#是不是在长方体内。只要用该点的x,y坐标和一半后的长、宽进行比较即可。这才是坐标对坐标。而两点
#之间长短距离则带有角度弧度的参加。因此偏离了主题

# disDigit1 = pow((x1-x0),2)
# print(f"Width is {disDigit1}")
# disDigit2 = pow((y1-y0),2)
# print(f"Hight is {disDigit2}")

# disDigit = (disDigit1 + disDigit2) ** 0.5
# print(disDigit)
if width >= x1 and hight >= y1:
      print(f"Point {x1,y1} is in the rectangle")
else:
      print(f"Point {x1,y1} is not in the rectangle")
import turtle as t
t.screensize(10,10)
t.speed(1)
t.penup()
t.goto(x0,y0)
t.dot(10,"red")
t.goto(-width,hight)
t.pendown()
t.forward(100)
t.right(90)
t.forward(50)
t.right(90)
t.forward(100)
t.right(90)
t.forward(50)

t.penup()
t.goto(x1,y1)
t.dot(10,"black")

t.done()

4.24

import random as r

cardNumber = r.randint(1,14)
cardColor = r.randint(1,5)

if cardColor == 1:
    cName =  "Hearts" 
elif cardColor == 2:
    cName =  "Spades" 
elif cardColor == 3:
    cName =  "Square" 
elif cardColor == 4:
    cName =  "Plim blossom" 


if cardNumber == 1:
    cNumber = "Ace"
elif cardNumber == 2:
    cNumber = "2"
elif cardNumber == 3:
    cNumber = "3"
elif cardNumber == 4:
    cNumber = "4"
elif cardNumber == 5:
    cNumber = "5"
elif cardNumber == 6:
    cNumber = "6"
elif cardNumber == 7:
    cNumber = "7"
elif cardNumber == 8:
    cNumber = "8"
elif cardNumber == 9:
    cNumber = "9"
elif cardNumber == 10:
    cNumber = "10"
elif cardNumber == 11:
    cNumber = "Jack"
elif cardNumber == 12:
    cNumber = "Queen"
elif cardNumber == 13:
    cNumber = "King"

print(f"The card you picked is the {cNumber} of {cName}")

4.25 时隔1年还是做不出来。公式是网上找的

# x1,y1,x2,y2,x3,y3,x4,y4 = eval(input("Enter x1,y1,x2,y2,x3,y3,x4,y4: "))

# judgeDigit = (x1 * x2) -(x3 * x4)

x1, y1 = eval(input("Enter point 1: "))
x2, y2 = eval(input("Enter point 2: "))
x3, y3 = eval(input("Enter point 3: "))
x4, y4 = eval(input("Enter point 4: "))

# 从网上下来的公式
# x = (x2 * y3 - y2 * x3) / (x1 * y2 - y1 * x2)
# y = (y1 * x3 - x1 * y3) / (x1 * y2 - y1 * x2)
# print(f"x is {x}")
# print(f"y is {y}")
# print("x = x1 + y1", x1 + y1)
# print("y = x2 + y2", x2 + y2)


if (x4-x3)*(y1-y2)+(y3-y4)*(x1-x2) == 0:
    print("The two lines are parallel")
else:
    y = ((y3-y4)*(y1-y2)*x3 + (y1-y2)*(x4-x3)*y3 + (y4-y3)* (y1-y2) * x2+(x2-x1)*(y4-y3)*y2) / ((x4-x3)*(y1-y2)+(y3-y4)*(x1-x2))
    # print(f"y is {y}")
    x = x2+(x1-x2)*(y-y2)/(y1-y2)
    # print((f"x is {x}"))
    print(f"The intersecting point is at {x,y}")

4.26

inteDigit = eval(input("Enter a three-digit integer: "))

a= inteDigit // 100
print(a)
b= inteDigit % 100 % 10
print(b)
if a == b:
    print(f"{inteDigit} is a palindrome")
else:
    print(f"{inteDigit} is not a palindrom")

4.27

import turtle as t

x, y = eval(input("Enter a point's x- and y-coordinates: "))

x1, y1 = 0, 0
x2, y2 = 200, 0
x3, y3 = 0, 100


# 判断三角形的方向

traSign = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)

print(traSign)

# 判断p点和三条线的三个关系

abSide = (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1)
caSide = (x1 - x3) * (y - y3) - (y1 - y3) * (x - x3)
bcSide = (x3 - x2) * (y - y3) - (y3 - y2) * (x - x3)

if (abSide * traSign > 0) and (caSide * traSign > 0) and (bcSide * traSign > 0):
    print(f"The point is in the triangle")
    t.goto(x2,y2)
    t.dot(6,"red")
    t.write(f"{x2,y2}")
    t.goto(x3,y3)
    t.dot(6,"red")
    t.write(f"{x3,y3}")
    t.goto(x1,y1)
    t.dot(6,"red")
    t.write(f"{x1,y1}")
    t.penup()
    t.goto(x,y)
    t.dot(6,"red")
    t.pendown()
    t.goto(x2,y2)
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.goto(x1,y1)
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.goto(x3,y3)



else:
    print(f"The point is not in the triangle")
    t.goto(x2,y2)
    t.dot(6,"red")
    t.write(f"{x2,y2}")
    t.goto(x3,y3)
    t.dot(6,"red")
    t.write(f"{x3,y3}")
    t.goto(x1,y1)
    t.dot(6,"red")
    t.write(f"{x1,y1}")
    t.penup()
    t.goto(x,y)
    t.dot(6,"red")
t.hideturtle()
t.done()

4.28 该题解题失败 。原第3版解题丢失。待以后再做把

import turtle as t
t.screensize(100, 100)
x1, y1, w1, h1 = eval(
    input("Enter r1's center x-,y-coordinates, width,and height: "))
x2, y2, w2, h2 = eval(
    input("Enter r2's center x-,y-coordinates, width,and height: "))


s1 = w1 * h1
s2 = w2 * h2

disDigit = ((pow((x2-x1), 2) + pow((y2-y1), 2))) ** 0.5

if s1 > s2 and w1 > disDigit and h1 > disDigit:
    print("r2 is inside r1")

elif w1 > disDigit and s1 != s2:
    print("r2 overlaps r1")

t.penup()
t.goto(x1, y1)
t.dot(6, "red")
t.write(f"{x1,y1}")
t.penup()
t.goto(-w1/2+x1, y1 + h1/2)
t.pendown()
t.forward(w1)
t.right(90)
t.forward(h1)
t.right(90)
t.forward(w1)
t.right(90)
t.forward(h1)

t.penup()
t.right(90)
t.goto(x2, y2)
t.dot(6, "red")
t.write(f"{x2,y2}")
t.penup()
t.goto(-w2/2+x2, y2 + h2/2)
t.pendown()
t.forward(w2)
t.right(90)
t.forward(h2)
t.right(90)
t.forward(w2)
t.right(90)
t.forward(h2)
t.hideturtle

t.done()

4.29

x1, y1, r1 = eval(
    input("Enter circle1's center x-,y-coordinates, and radius: "))
x2, y2, r2 = eval(
    input("Enter circle2's center x-,y-coordinates, and radius: "))


disDigit = (pow((x2-x1), 2) + pow((y2-y1), 2)) ** 0.5
# print(disDigit)
if disDigit < r1 and r2 < r1:
    print("circle2 is inside circle1")
elif disDigit > r1+r2:
    print("circle2 does not overlap circle1")
else:
    print("circle2 overlap circle1")

4.30

import time

timeZone = eval(input("Enter the time zone offset to GMT: "))
currentTime = time.time()
totalSeconds = int(currentTime)  # 整数化总共秒数 从1970-1-1开始
# print(totalSeconds)
currenSecond = totalSeconds % 60  # 当前的秒数。是用60对总秒数进行除余得到的余数为当前秒数
# print(currenSecond)
totalMinutes = totalSeconds // 60  # 总分钟数,是用总的秒数 整除60得出
# print(totalMinutes)
currentMinute = totalMinutes % 60  # 当前分钟数,是用总的分钟 除余60 得出的。余数即为当前分钟数
# print(currentMinute)
totalHours = totalMinutes // 60  # 总小时。是用总的分钟数整除60得出
# print(totalHours)
currentHour = totalHours % 24

# 先对总秒数整数化。分别得出总秒数、总分钟、总小时。然后这些条件。得出当前小时和秒数

tz= currentHour + timeZone

print(f"tz is {tz}")
if  tz<=12:
    timeShow = "AM"
    aStime = tz
elif tz > 12:
    timeShow = "PM"
    aStime = tz % 12

print("Current time is", aStime, ":",
      currentMinute, ":", currenSecond, timeShow)
# 重新修正了一下之前第1和第2刷时用过的办法。我觉得第1和第2刷的时候自己的编程能力还是可以的。
#应该多刷。只是 从第3章开始刷的次数少了,所以感觉对题的处理能力减低

4.31

import turtle

x0, y0, x1, y1, x, y = eval(
    input("Enter coordinates for the three points p0,p1,and p2: "))

judeDigit = (x1 - x0) * (y - y0) - (y1 - y0) * (x - x0)  # 此公式是判断点是否在线的那个方向上。而无法判断点是不是在线上。
if judeDigit > 0:  # 此处大于0显示点在线的左侧和结果一直
    print(f"p2 is on the left side of the line from PO to P1")
elif judeDigit == 0:  # 此处为正确,即等于0说明点在线上。原书中的答案存在错误
    print("p2 is on the same line from p0 to p1")
elif judeDigit < 0:
    print(f"p2 is on the right side of the line from P0 to P1")

# 怀疑书中给的答案不对。 考虑在下面用turtle来一次验证

turtle.pensize(2)
turtle.penup()
turtle.goto(x0, y0)
turtle.write(f"P0 {x0, y0}")
turtle.dot(6, "blue")
turtle.pendown()
turtle.goto(x1, y1)
turtle.write(f"P1 {x1, y1}")
turtle.dot(6, "blue")

turtle.penup()
turtle.goto(x, y)
turtle.dot(6, "red")

turtle.done()

4.32

import turtle

x0, y0, x1, y1, x, y = eval(
    input("Enter coordinates for the three points p0,p1,and p2: "))

judeDigit = (x1 - x0) * (y - y0) - (y1 - y0) * (x - x0)  # 此公式是判断点在线某个方向。而不是判定点是不是在线上。


if judeDigit == 0:  # 此处为正确,即等于0说明点在线上。原书中的答案存在错误
    print(f"{(x,y)} is on the line segment from {(x0,y0)} to {(x1,y1)}")
else:
    print(f"{(x,y)} is not on the line segment from {(x0,y0)} to {(x1,y1)}")

# 怀疑书中给的答案不对。 考虑在下面用turtle来一次验证

turtle.pensize(2)
turtle.penup()
turtle.goto(x0, y0)
turtle.write(f"P0 {x0, y0}")
turtle.dot(6, "blue")
turtle.pendown()
turtle.goto(x1, y1)
turtle.write(f"P1 {x1, y1}")
turtle.dot(6, "blue")

turtle.penup()
turtle.goto(x, y)
turtle.dot(6, "red")

turtle.done()

4.33

integer = eval(input("Enter a decimal value(0 to 15): "))
if integer <= 15:
    print(chr(integer))
    print("The hex value is", format(integer, "X"))  # 在format里 “x"代表16进制。当我选择"X"则显示的结果是大写的16进制。2021-7-6
else:
    print("Invalid input")

4.34 hex to decimal number

a = input("Enter a hex character: ")

if a == 1:
    print("The decimal value is 1")
elif a == 2:
    print("The decimal value is 2")
elif a == 3:
    print("The decimal value is 3")
elif a == 4:
    print("The decimal value is 4")
elif a == 5:
    print("The decimal value is 5")
elif a == 6:
    print("The decimal value is 6")
elif a == 7:
    print("The decimal value is 7")
elif a == 8:
    print("The decimal value is 8")
elif a == 9:
    print("The decimal value is 9")
elif a == "A":
    print("The decimal value is 10")
elif a == "B":
    print("The decimal value is 11")
elif a == "C":
    print("The decimal value is 12")
elif a == "D":
    print("The decimal value is 13")
elif a == "E":
    print("The decimal value is 14")
elif a == "F":
    print("The decimal value is 15")
elif a == "G":
    print("Invalid input")

4.35

import turtle as tu

x0, y0, x1, y1, x2, y2 = eval(input("Enter three points axis x,y: "))

disNumber = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)

tu.penup()
tu.goto(x0, y0)
tu.write(f"p0{x0, y0}")
tu.pendown()
tu.goto(x1, y1)
tu.write(f"p1{x1, y1}")

tu.penup()
tu.goto(x2, y2)

if disNumber > 0:
    tu.write(f"p2{x2, y2},P2 is on the left side of the line")
    print(f"p2{x2, y2},P2 is on the left side of the line")
elif disNumber == 0:
    tu.write(f"p2{x2, y2},P2 is on the same line ")
    print(f"p2{x2, y2},P2 is on the same line ")
else:
    tu.write(f"p2{x2, y2},P2 is on the right side of the line")
    print(f"p2{x2, y2},P2 is on the right side of the line")

tu.done()

4.36

import turtle as tu

x, y = eval(input("Rectangle x,y: "))
x1, y1, x2, y2, x3, y3, x4, y4 = -50, 25, 50, 25, 50, -25, -50, -25
tu.dot(6, "red")
tu.penup()
tu.goto(x1, y1)
tu.dot(5, "green")
tu.write(f"p1{x1, y1}")
tu.pendown()
tu.goto(x2, y2)
tu.dot(5, "green")
tu.write(f"p2{x2, y2}")
tu.goto(x3, y3)
tu.dot(5, "green")
tu.write(f"p3{x3, y3}")
tu.goto(x4, y4)
tu.dot(5, "green")
tu.write(f"p4{x4, y4}")
tu.goto(x1, y1)

tu.penup()
tu.goto(x, y)
tu.pendown()
tu.dot(6, "blue")

disNumber1 = (x2 - x1) * (y - y1) - (x - x1) * (y2 - y1)
disNumber2 = (x3 - x2) * (y - y2) - (x - x2) * (y3 - y2)
disNumber3 = (x4 - x3) * (y - y3) - (x - x3) * (y4 - y3)
disNumber4 = (x1 - x4) * (y - y1) - (x - x4) * (y1 - y4)
if disNumber1 > 0 and disNumber2 > 0 and disNumber3 > 0 and disNumber4 > 0:
    print("The point is not inside the rectangle")
    tu.penup()
    tu.goto(-50, -60)
    tu.write("The point is not inside the rectangle")
else:
    print("The point is inside the rectangle")
    tu.penup()
    tu.goto(-50, -60)
    tu.write("The point is inside the rectangle")

tu.done()

4.38

import turtle as tu

# x1, y1, x2, y2, w1, h1, w2, h2 = eval(input("Enter Two rectangle axis x,y,high,weigh:"))
# x1, y1, x2, y2, w1, h1, w2, h2 = 45, 106, 76, 85, 100, 50, 200, 30
x1, y1, x2, y2, w1, h1, w2, h2 = 55, 136, 30, 160, 250, 80, 400, 60
a1x = x1 - w1 / 2
a1y = h1 / 2 + y1
a2x = a1x + w1
print(a2x)
a2y = a1y
a3x = a2x
a3y = a2y - h1
a4x = a3x - w1
a4y = a3y

b1x = x2 - w2 / 2
print(b1x)
b1y = h2 / 2 + y2
b2x = b1x + w2
b2y = b1y
b3x = b2x
b3y = b2y - h2
b4x = b3x - w2
b4y = b3y

tu.penup()
tu.goto(x1, y1)
tu.dot(6, "blue")
tu.goto(a1x, a1y)
tu.dot(6, "red")
# tu.goto(-10,75)
# tu.dot(6,"blue")
tu.pendown()
tu.goto(a2x, a2y)
tu.dot(6, "yellow")

tu.goto(a3x, a3y)
tu.dot(6, "yellow")

tu.goto(a4x, a4y)
tu.dot(6, "yellow")

tu.goto(a1x, a1y)

tu.penup()
tu.goto(x2, y2)
tu.dot(6, "red")

tu.goto(b1x, b1y)
tu.pendown()
tu.goto(b2x, b2y)
tu.goto(b3x, b3y)
tu.goto(b4x, b4y)
tu.goto(b1x, b1y)

dis1 = (a1x - a2x) * (y2 - a2y) - (x2 - a2x) * (a1y - a2y)
dis2 = (a2x - a3x) * (y2 - a3y) - (x2 - a3x) * (a1y - a3y)
dis3 = (a3x - a4x) * (y2 - a4y) - (x2 - a4x) * (a1y - a4y)
dis4 = (a4x - a1x) * (y2 - a1y) - (x2 - a1x) * (a1y - a1y)

if dis1 > 0 and dis2 > 0 and dis3 > 0 and dis4 > 0:
    if w1 * h1 < w2 * h2:
        print("r2 overlaps r1")
        tu.penup()
        tu.goto(300, 300)
        tu.write("r2 overlaps r1")
    else:
        print("r2 is inside r1")
        tu.penup()
        tu.goto(300, 300)
        tu.write("r2 is inside r1")
else:
    if w2 * h2 < w1 * h1:
        print("r2 does not overlap r1")
        tu.penup()
        tu.goto(300, 300)
        tu.write("r2 does not overlap r1")
    else:
        print("r2 overlaps r1")
        tu.penup()
        tu.goto(50, -10)
        tu.write("r2 overlaps r1")


tu.done()

4.39

import turtle as tu

# x1, y1, x2, y2, r1, r2 = eval(input("Circle1 and Circle2 is  axis X,Y,radius: "))
x1, y1, x2, y2, r1, r2 = 60, 80, 120, 16, 90, 200
ry1= y1 +r1
ry2 = y2 +r2
dis = ((x2 - x1) ** 2 + (ry2 - ry1) ** 2) ** 0.5

print(dis)

tu.penup()
tu.goto(x1, y1)
tu.pendown()
tu.circle(r1)
tu.penup()
tu.goto(x2, y2)
tu.pendown()
tu.circle(r2)
if dis > r1 + r2:
    print("Circle1 does not overlap Circle2")
    tu.penup()
    tu.goto(200, -200)
    tu.write("Circle1 does not overlap Circle2")

elif dis <= r1 and r2 < r1:
    print("Circle2 is inside Circle1")
    tu.penup()
    tu.goto(200, -200)
    tu.write("Circle2 is inside Circle1")
elif dis <= r2 and r1 < r2:
    print("Circle1 is inside Circle2")
    tu.penup()
    tu.goto(200, -200)
    tu.write("Circle1 is inside Circle2")
elif r1 + r2 > dis >= r1:
    print("Circle2 overlaps Circle1")
    tu.penup()
    tu.goto(200, -200)
    tu.write("Circle2 overlaps Circle1")

tu.done()

2022-12-1重新整理。好几分钟。收藏三连哦。同学们

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电饭叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值