Python语言程序设计Y.Daniel Liang练习题Chapter4

ckp4.1

 

'''
<小于
<=不大于
>大于
>=不小于
==等于
!=不等于
'''

ckp4.2

i = int(True)
j = int(False)

b1 = bool(4)
b2 = bool(0)
print(i, j, b1, b2)

ckp4.3-4.6

import random
# output 0<=i<20
i = int(random.random() * 20)
print(i)

# output 10<=j<20
j = int(random.random() * 10) + 10
print(j)

# output 10<=k<=50
k = random.randint(10, 50)
print(k)

# output 0 or 1
m = random.randint(0, 1)
print(m)

ckp4.10

number1 = 30
if number1 % 2 == 0:
    print(number1, "is even.")

print(number1, "is odd.")

if number1 % 2 == 0:
    print(number1, "is even.")
else:
    print(number1, "is odd.")

#
number2 = 35
if number2 % 2 == 0:
    print(number2, "is even.")

print(number2, "is odd.")

if number2 % 2 == 0:
    print(number2, "is even.")
else:
    print(number2, "is odd.")

ckp4.20

x = 1
print(True and (3 > 4))
print(not (x > 0) and (x > 0))
print((x > 0) or (x == 0))
print((x != 0) or (x == 0))
print((x >= 0) or (x < 0))
print((x != 1) == (not (x == 1)))  # "not" pri than "=="

ckp4.21-4.22

import random
num = random.randint(-100, 300)
print(num)
if (num >= 1) and (num <= 100):
    print("True")
else:
    print("False")

if (num >= 1) and (num <= 100) or (num < 0):
    print("true")
else:
    print("False")

ckp4.23

x = 4
y = 5

a = (x >= y) and (y >= 0)
b = (x <= y) and (y >= 0)
c = (x != y) and (y == 5)
d = (x != 0) or (x == 0)
print(a, b, c, d)

ckp4.24

'''
a = (x >= 1) and (x < 10)
b = (1 <= x) and (x < 10)
这两个是等价的
'''

ckp4.25

ch = 'A'
if (ch >= 'A') and(ch <= 'Z'):
    print("True")
else:
    print("False")

ch = 'p'
if (ch >= 'A') and(ch <= 'Z'):
    print("True")
else:
    print("False")

ch = 'E'
if (ch >= 'A') and(ch <= 'Z'):
    print("True")
else:
    print("False")

ch = '5'
if (ch >= 'A') and(ch <= 'Z'):
    print("True")
else:
    print("False")

ckp4.26

x, y, z = eval(input("Enter three numbers: "))

print("(x < y and y < z) is", x < y and y < z)
print("(x < y or y < z) is", x < y or y < z)
print("not (x < y) is", not (x < y))
print("(x < y < z) is", x < y < z)
print("not (x < y < z) is", not (x < y < z))

ckp4.27-4.30

# ckp4.27
age = eval(input("Please enter your age:"))
print((age >= 13) and (age <= 18))
# ckp4.28
weight, height = eval(input("Please enter your weight and height:"))
print((weight >= 50) or (height >= 160))
# ckp4.29
print((weight >= 50) and (height >= 160))
# ckp4.30
print(((weight >= 50) and (height <= 160)) or ((weight <= 50) and (height >= 160)))

ckp4.31

x, y, z = eval(input("Enter three numbers: "))
print("sorted" if x < y and y < z else "not sorted")

ckp4.32

ages = eval(input("Please enter ages: "))
ticketPrince = 16 if ages >= 16 else 10

print(ticketPrince)
count = eval(input("Please enter count: "))
print(count if count % 10 == 0 else count, end = "ok") # end表示以双引号里的内容结尾

'''if count % 10 == 0:
    print(count)
else:
    print(count, end = "ok")
'''

ckp4.33

x, scale = eval(input("x, scale: "))
if x > 10:
    score = 3 * scale
else:
    score = 4 * scale

income = eval(input("income: "))
if income > 10000:
    tax = income * 0.2
else:
    tax = income * 0.17 + 1000

number, i, j = eval(input("number, i, j: "))
if number % 3 == 0:
    print(i)
else:
    print(j)

ckp4.34

''' 优先级排序(由高到低)
+,-(加减); **(幂); not; *,/,//,%(乘除,取整,取余); +,-(正负号); <,<=,>=(比较);
 ==,!=(等于和不等于); and; or; =,+=,-=,*=,/=,//=,%=(双目运算符)
'''

print(True or True and False)
print(True and True or False)

ckp4.35

#  All the binary operators except = are left-associative?
#  对!!!!

ckp4.36

print(2 * 2 - 3 > 2 and 4 - 2 > 5)
print(2 * 2 - 3 > 2 or 4 - 2 > 5)

ckp4.37

'''
Is (x > 0 and x < 10) the same as ((x > 0) and (x < 10))?    Yes!!!
Is (x > 0 or x < 10) the same as ((x > 0) or (x < 10))?      Yes!!!
Is (x > 0 or x < 10 and y < 0) the same as (x > 0 or (x < 10 and y < 0))?    Yes!!!
'''


list4.1

import random

number1 = random.randint(0, 9)
number2 = random.randint(0, 9)
answer = eval(input("What is "+ str(number1) + "+" +str(number2) + "?"))
print(number1, "+", number2, "=", answer, "is", number1 + number2 == answer)

list4.4

import random

# 1. Generate two random single-digit integers
number1 = random.randint(0, 9)
number2 = random.randint(0, 9)

# 2. If number1 < number2, swap number1 with number2
if number1 < number2:
    number1, number2 = number2, number1 # Simultaneous assignment

# 3. Prompt the student to answer "What is number1 - number2?"
answer = eval(input("What is "+ str(number1) + " - " + str(number2) + "? "))

# 4. Check the answer and display the result
if number1 - number2 == answer:
    print("You are correct!")
else:
    print("Your answer is wrong.\n", number1, '-', number2, "is", number1 - number2, '.')

list4.6

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

height = eval(input("Enter height in inches: "))

KILOGRAMS_PER_POUND = 0.45359237
METERS_PER_INCH = 0.0254

weightInKilograms = weight * KILOGRAMS_PER_POUND
heightInMeters = height * METERS_PER_INCH
bmi = weightInKilograms / (heightInMeters * heightInMeters)

print("BMI is", format(bmi, ".2f"))
if bmi <18.5:
    print("Underweight")
elif bmi < 25:
    print("Normal")
elif bmi < 30:
    print("Overweight")
else:
    print("obese")

list4.7_ComputeTax

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

tax = 0

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:  # Compute tax for married file jointly
    print("Left as exercise")
elif status == 2:  # Compute tax for married separately
    print("Left as exercise")
elif status == 3:  # Compute tax for head of household
    print("Left as exercise")
else:
    print("Error: invalid status")
    sys.exit()

list4.11_PointInCircle

import turtle

x1, y1 = eval(input("Enter the center of a circle x, y: "))
radius = eval(input("Enter the radius of the circle: "))
x2, y2 = eval(input("Enter a point x, y: "))

# Draw the circle
turtle.penup() # Pull the pen up
turtle.goto(x1, y1 - radius)
turtle.pendown() # Pull the pen down
turtle.circle(radius)
# Draw the point
turtle.penup()
turtle.goto(x2, y2)
turtle.pendown()
turtle.begin_fill()
turtle.color("red")
turtle.circle(3)
turtle.end_fill()

# Display the status
turtle.penup() # Pull the pen up
turtle.goto(x1 - 70, y1 - radius - 20)
turtle.pendown()

d = ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) ** 0.5
if d <= radius:
    turtle.write("The point is inside the circle")
else:
    turtle.write("The point is outside the circle")

turtle.hideturtle()

turtle.done()

sec4.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)

sec4.4

import random

number1 = random.randint(0, 100)
number2 = random.randint(0, 100)
answer = eval(input("What is "+ str(number1) + "+" +str(number2) + "?"))
if number1 + number2 ==answer:
    print("Your answer is correct!")
else:
    print("Your answer is False!")

sec4.8

a, b, c = eval(input("Please enter 3 numbers: "))
if a < b:
    a, b = b, a
if a < c:
    a, c = c, a
if b < c:
    b, c = c, b
print(c, b, a)

sec4.12

num = eval(input("Please enter an interger: "))
print("Is 10 divisible by 5 and 6 ? ")
print("True" if (num % 5 == 0) and (num % 6 == 0) else "False")
print("Is 10 divisible by 5 or 6? ")
print("True" if (num % 5 == 0) or (num % 6 == 0) else "False")
print("Is 10 divisible by 5 or 6, but not both? ")
print("True" if ((num % 5 == 0) and (num % 6 != 0)) or ((num % 5 != 0) and (num % 6 == 0)) else "False")

sec4.16

import random

i = 'A'
j = random.randint(0, 25)
k = ord(i) + j
# ord函数把字符转化成数值
# chr函数把数字转化成字符
print(chr(k))

sec4.19

a, b, c = eval(input("Enter three edges: "))
sum = a + b + c
# rank the numbers from lowest to top, a > b > c
if a < b:
    a, b = b, a
if a < c:
    a, c = c, a
if b < c:
    b, c = c, b
print("The perimeter is " + str(sum) if (b + c) > a else "The input is invalid")


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值