Python判断语句

分享
手机游戏开发者 2024-9-8 11:53:59 54 0 来自 中国
一、判断的界说:


  • 假如 条件满意,才华做某件变乱,
  • 假如 条件不满意,就做别的一件变乱,大概什么也不做
二、if 语句语法布局


  • 尺度if条件语句的语法 缩进:python代码的层级关系
  • 缩进一样平常加4个空格


  • 假如表达式的值 非0 大概为布尔值 True,则代码组 if_suite 被实行;否则就去实行 else_suite
  • 代码组是一个 python术语,它由一条或多条语句构成,表示一个子代码块
空范例判断


  • 验证各范例空的判断
input='判断" "的结果:';if " ": # True    print(input,"True")else:    print(input,"False")input="判断[]的结果:";if []: # False    print(input,"True")else:    print(input,"False")input="判断[False]的结果:";if [False]: # True    print(input,"True")else:    print(input,"False")input="判断None的结果:";if None: # False    print(input,"True")else:    print(input,"False")

  • 输出结果
[root@Python day02]# python3 demo01_if.py 判断零的结果: False判断非零的结果: True判断""的结果: False判断" "的结果: True判断[]的结果: False判断[False]的结果: True判断None的结果: False练习 1:判断正当用户

需求


  • 提示用户输入用户名和暗码
  • 获得到相干信息后,将其生存在变量中
  • 假如用户输的用户名为 bob,暗码为 123456,则输出 Login successful,否则输出 Login incorrect
# login2.py是文件名,可以修改[root@localhost day02]# vim login2.py  # 界说两个变量 username 和 password,分别从键盘获取用户名和暗码username = input('请输入用户名:')password = input('请输入暗码:')# 在python中即是号用 == 表示;input()函数获取的值都是字符范例,暗码要用引号引起来if username == 'bob' and password == '123456': print('Login successful')else: print('Login inorrect')print(" Login end ")三、if 扩展语句:


  • 语法布局:


四、简写方式:

示例:


  • “乐成的返回值” if “判断语句” else “失败的返回值”
print("乐成" if 10 % 2 == 0 else "失败")练习 2:编写判断结果的步伐

需求


  • 假如结果大于60分,输出“及格”
  • 假如结果大于70分,输出“良”
  • 假如结果大于80分,输出“好”
  • 假如结果大于90分,输出“精良”
  • 否则输出“你要积极了”
编写判断结果的步伐

[root@localhost day02]# vim demo02_if.py  # demo02_if.py是文件名,可以修改score = int(input('分数:'))#步伐的实行,是从上往下实行的;#先判断大的数值,不满意再继承往下判断,可以简化步伐代码if score >= 90: print('精良')elif score >= 80: print('好')elif score >= 70: print('良')elif score >= 60: print('及格')else: print('你要积极了!!!')练习 3:编写石头铰剪布小游戏

需求:


  • 计算机随机出拳
  • 玩家本身决定怎样出拳
  • 代码尽量简化
方法一:编写铰剪石头布小游戏**

[root@localhost day02]# vim test01_if.py  # test01_if.py是文件名,可以修改# 导入随机数的模块 randomimport random# 界说列表all_choices,存储出拳的每个选择all_choices = ['石头','铰剪','布']# random模块的choice方法,可以从变量中随机选出一个元素,随机出来的元素为电脑的出拳computer = random.choice(all_choices)# 利用input()函数,获取用户的出拳选择player = input('请出拳(石头/铰剪/布):')# 利用函数print()打印出电脑和玩家的出拳选择print("Your choice:" + player + ", Computer's choice: " + computer)# 当玩家出拳为【石头】时,电脑的每一种出拳选择都要和【石头】举行比力,打印结果if player == '石头': if computer == '石头': print('平手') elif computer == '铰剪': print('You WIN!!!') else: print('You LOSE!!!')# 当玩家出拳为【铰剪】时,电脑的每一种出拳选择都要和【铰剪】举行比力,打印结果elif player == '铰剪': if computer == '石头': print('You LOSE!!!') elif computer == '铰剪': print('平手') else: print('You WIN!!!')# 当玩家出拳为【布】时,电脑的每一种出拳选择都要和【布】举行比力,打印结果else: if computer == '石头': print('You WIN!!!') elif computer == '铰剪': print('You LOSE!!!') else: print('平手')方法二:(win_list)

# test02_if.py是文件名,可以修改[root@localhost day02]# vim test02_if.py # 导入随机数的模块 randomimport random# 界说列表all_choices,存储出拳的每个选择all_choices = ['石头','铰剪','布']# 界说列表win_list, 将玩家赢电脑的选择,提宿世存起来# 每个子列表中,玩家为第一个元素,电脑为第二个元素win_list = [  ['石头','铰剪'], ['铰剪','布'], ['布','石头']   ]# random模块的choice方法,可以从变量中随机选出一个元素# 随机出来的元素为电脑的出拳computer = random.choice(all_choices)# 利用input()函数,获取用户的出拳选择player = input('请出拳(石头/铰剪/布):')# 利用函数print()打印出电脑和玩家的出拳选择print("Your choice:" + player + ", Computer's choice: " + computer)# 假如玩家和电脑的出拳雷同,则平手if player == computer: print('平手')# 假如[玩家,电脑]的列表,在win_list中,则可判断玩家赢,由于在win_list中界说了玩家赢得各种大概性 elif [player,computer] in win_list: print('You WIN!!!')# 玩家不是赢,那么就是输了,玩家全部赢得大概性已经在win_list界说了 else: print('You LOSE!!!')
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-19 11:40, Processed in 0.123020 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表