kkpiece 2020-06-16
#-*- coding = utf-8 -*-#@Time : 2020/6/16 12:02#@Authur : Agur Zheng#@File : Test_if.py#@Software : PyCharm# if后边跟判断条件,为真继续执行,为假转到下一个elif执行新的判断或者else继续执行# Python中任何非0,非空为True;0或者None表示Fales# Python中以相同缩进表示相同范围if True: print("条件为真时执行打印")else: print("条件不为真时执行此打印")if 18: # 以非0值表示真 print("条件为真时执行打印")else: print("条件不为真时执行此打印")name ="Agur"if name: # 以非空表示真 print("条件为真时执行打印")else: print("条件不为真时执行此打印")if False: print("条件为真时执行打印")else: print("条件不为真时执行此打印")if 0: print("条件为真时执行打印")else: print("条件不为真时执行此打印")age = None # 关于None的更多信息,http://c.biancheng.net/view/5659.htmlif age: print("条件为真时执行打印")else: print("条件不为真时执行此打印")# 在Python中使用from...impor语句来导入模块及函数# from somemodule import somefunction 从某模块中导入某函数# from somemodule import somefunction1, somefunction2,... 从某模块中导入多个函数# from somemodule import \* 从模块中导入全部函数# import somemodule 导入某模块import random # 引入随机库print(random.randint(0,2)) # 随机生成0~2的整数
#-*- coding = utf-8 -*- #@Time : 2020/6/16 13:29 #@Authur : Agur Zheng #@File : if_exercise.py #@Software : PyCharm ‘‘‘ 本程序实现以下功能: 用户输入数字0,1,2(分别对应石头,剪刀,布) 系统随机生成数字0,1,2 比较两个数字,规则是:石头>剪刀;剪刀>布;布>石头 打印信息显示用户输入结果,随机生成结果,比较结果 ‘‘‘ import random # 让用户输入,并显示输入结果 num_user = input("请输入0,1,2(分别对应石头,剪刀,布):") num_1 = int(num_user) if num_1 == 0: user = "石头(0)" elif num_1 == 1: user = "剪刀(1)" elif num_1 == 2: user = "布(2)" print("你的输入为%s" %user) # 系统随机生成,并显示生成结果 sys_num = random.randint(0,2) if sys_num == 0: sys_crea = "石头(0)" elif sys_num == 1: sys_crea = "剪刀(1)" elif sys_num == 2: sys_crea = "布(2)" print("随机生成的结果为%s" %sys_crea) # 比较用户输入和系统生成的结果,输出比较结果 result_1 = "哈哈,平局" result_2 = "哈哈,你赢了" result_3 = "哈哈,你输了" if num_1 == 0 : if sys_num == 0: print(result_1) elif sys_num == 1: print(result_2) elif sys_num == 2: print(result_3) elif num_1 == 1: if sys_num == 0: print(result_3) elif sys_num == 1: print(result_1) elif sys_num == 2: print(result_2) elif num_1 == 2: if sys_num == 0: print(result_2) elif sys_num == 1: print(result_3) elif sys_num == 2: print(result_1)
还需考虑当用户输入要求的值以外的值时候应如何处理