python_day02_练习
选择
布尔类型、数值和表达式
- 注意:比较运算符的相等是两个等号,一个等到代表赋值
- 在Python中可以用整型0来代表False,其他数字来代表True
- 后面还会讲到 is 在判断语句中的用发
Out[64]:
EP:
- 产生两个随机整数number1和number2,然后显示给用户,使用户输入数字的和,并判定其是否正确
- 进阶:写一个随机序号点名程序
In [69]:
number_1 = random.randrange(0,10)
number_2 = random.randrange(0,10)
while 1:
sum_ = eval(input('>>'))
if sum_ == (number_1 + number_2):
print('Congratulations! Correct~')
else:
print('Sorry~SB.')
. . .
if语句
-
如果条件正确就执行一个单向if语句,亦即当条件为真的时候才执行if内部的语句
-
Python有很多选择语句:
- 单向if
- 双向if-else
- 嵌套if
- 多向if-elif-else
-
注意:当语句含有子语句的时候,那么一定至少要有一个缩进,也就是说如果有儿子存在,那么一定要缩进
-
切记不可tab键和space混用,单用tab 或者 space
-
当你输出的结果是无论if是否为真时都需要显示时,语句应该与if对齐
In [72]:
input_ = eval(input('>>'))
if input_ > number:
print('太大啦')
if input_ < number:
print('太小啦')
if number == input_:
print('正好')
print('不要灰心')
李文浩相亲测试树
年龄
老 年轻
拜拜
帅 否 是 考虑一下 老婆 没有 有 马上结婚 回家的诱惑
代码写不出来的立马分手,从此社会上有多出一个渣男/渣女.
In [4]:
age = input('年轻嘛[y/n]')
if age == 'y':
handsome = input('帅否[y/n]')
if handsome == 'y':
wife = input('有没有老婆[y/n]')
if wife == 'y':
print('回家的诱惑')
else:
print('立马结婚')
else:
print('考虑一下')
else:
print('拜拜~')
EP:
- 用户输入一个数字,判断其实奇数还是偶数
- 进阶:可以查看下4.5实例研究猜生日
双向if-else 语句
- 如果条件为真,那么走if内部语句,否则走else内部语句
EP:
- 产生两个随机整数number1和number2,然后显示给用户,使用户输入数字,并判定其是否正确,如果正确打印“you‘re correct”,否则打印正确错误
嵌套if 和多向if-elif-else
In [ ]:
if score >= 80:
gread = 'B'
elif score>=90:
gread = 'A'
In [9]:
tizhong = eval(input('体重'))
shengao = eval(input('身高'))
BMI = tizhong / shengao ** 2
if BMI<18.5 :
print('超清')
elif 18.5<=BMI<25 :
print('标准')
elif 25<=BMI<30 :
print('超重')
else:
print('超级无敌胖')
逻辑运算符
EP:
- 判定闰年:一个年份如果能被4整除但不能被100整除,或者能被400整除,那么这个年份就是闰年
- 提示用户输入一个年份,并返回是否是闰年
- 提示用户输入一个数字,判断其是否为水仙花数
实例研究:彩票
In [16]:
import random
In [25]:
number = random.randint(10,99)
print(number)
N = input('>>')
number_shi = number // 10
number_ge = number % 10
if N[0] == '0':
N_shi = 0
else:
N_shi = int(N) // 10
N_ge = int(N) % 10
if number == int(N):
print('10000')
# elif (number_shi == N_shi or number_shi==N_ge) and (number_ge == N_shi or number_ge==N_ge):
elif number_shi + number_ge == N_shi + N_ge:
print('3000')
elif (number_ge ==N_ge or number_ge == N_shi) or (number_shi == N_ge or number_shi == N_shi):
print('1000')
In [3]:
a = "05"
a[0]
Out[3]:
In [22]:
05 // 10
Out[22]:
In [14]:
Number = eval(input('>>'))
bai = Number // 100
shi = Number // 10 % 10
ge = Number % 10
if bai**3 + shi **3 + ge **3 == Number:
print('水仙花')
else:
print('不是水仙花'
In [15]:
223 // 10
Out[15]:
Homework
In [3]:
import math
a,b,c = map(float,input('Enter a, b, c :').split(','))
if b **2 - 4 *a * c > 0:
r1 = (-b + math.sqrt (b **2 - 4 * a *c) ) /(2 *a)
r2 = (-b - math.sqrt ( b **2 - 4 * a *c )) /(2 *a)
print('The roots are %f and The roots are %f' % (r1, r2))
elif b **2 - 4 *a * c == 0:
r3 = (-b) /(2 *a)
print('r3:%d' % r3)
else:
print('The equation has no real roots')

In [4]:
import random
a =random.randrange(0,99)
b =random.randrange(0,99)
print(a,b)
c = int(input('请输入两个数的和:'))
if c == a + b :
print('程序为真')
else:
print('程序为假')

- 3
In [22]:
today = int(input("Enter today's day:"))
future = int(input("Enter the number of days elapsed since today:"))
week = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
futureweek = today + future
a = futureweek % 7
print("Today is %s and the future day is %s"%(week[today],week[a]))
- 4
In [5]:
a,b,c = map(int,input('Enter a, b, c :').split(','))
if a < b < c:
print(a,b,c)
elif a < c < b:
print(a,c,b)
elif b< a< c:
print(b,a,c)
elif b < c < a:
print(b,c,a)
elif c < a < b:
print(c,a,b)
else:
print(c,b,a)
- 5
In [6]:
a,b = map(float,input('Enter weight and price 1:').split(','))
c,d = map(float,input('nter weight and price 2 :').split(','))
e =b / a
f = d / c
print(e,f)
if e > f:
print('package 2 has better price')
else:
print('package 1 has better price')

- 6
In [23]:
a,b = map(float,input('请输入年和月份: ').split(','))
if a % 4 == 0 and a % 100 != 0 or a % 400 == 0:
c = 29
else:
c = 28
if b == 1 or b == 3 or b == 5 or b == 7 or b == 8 or b == 10 or b == 12:
d = 30
elif b == 2:
d = c
else:
d = 31
print( a,'年', b,'月份有', d,'天')

- 7
In [7]:
import numpy as np
res1 = np.random.choice(['正面','反面'])
print(res1)
res2 = input('请输入“正面,反面”')
if res1 == res2:
print('猜测正确')
else:
print('猜测错误')

- 8
In [10]:
import numpy as np
res1 = np.random.choice(['0','1','2'])
res2 = input('请输入“0,1,2”')
print('scissor ({}) ,rock ({}) '.format(res1,res2))
if res1 == '0' and res2 == '1':
print('res1 胜')
elif res1 == '1' and res2 == '0':
print('res2 胜')
elif res1 == '0' and res2 == '2':
print('res1 胜')
elif res1 == '2' and res2 == '0':
print('res2 胜')
elif es1 == '1' and res2 == '2':
print('res1 胜')
elif es1 == '2' and res2 == '1':
print('res2 胜')
else:
print('平局')

- 9
In [25]:
year = int(input('Enter year:(e.g.,2008):'))
m = int(input('Enter month:1-12:'))
q = int(input('Enter the day of the month:1-31:'))
week = ['Sunday','Monday','Thursday','Wednesday','Thursday','Friday','Saturday']
if m == 1 or m == 2:
year= year - 1
m = m+12
k = year % 100
j = year / 100
h = int(q + (26 * (m+1))/10 + k +(k/4) + (j/4) + (5*j)) % 7
print('Day of the week is %s'%(week[h]))
else:
m = m+12
k = year % 100
j = year / 100
h = int(q + (26 * (m+1))/10 + k +(k/4) + (j/4) + (5*j)) % 7
print('Day of the week is %s'%(week[h]))

- 10
In [20]:
import numpy as np
res1 = np.random.choice(['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King'])
res2 = np.random.choice(['梅花''红桃','方片','黑桃'])
print('The card you picked is the ',res1,'of',res2)

- 11
In [18]:
shu = int(input('请输入一个三位数整数:'))
a = shu // 100
b = shu % 10
if a == b:
print(shu,'是一个回文数')
else:
print(shu,'不是一个回文数')
- 12
In [16]:
import math
a,b,c= map(float,input('Enter three edges:').split(','))
if a + b > c and a + c > b and b + c > a:
print('The perimeter is %d' % (a + b + c))
p = (a + b + c) / 2
else:
print('不能构成三角形')
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 程序员转型AI:行业分析
· 深入集成:使用 DeepSeek SDK for .NET 实现自然语言处理功能
· 为DeepSeek添加本地知识库
· .NET程序员AI开发基座:Microsoft.Extensions.AI