Python知识点1:基本输入输出、条件判断语句(if)

tech2022-08-09  141

Python知识点1:基本输入输出、条件判断语句(if) b站学习:https://www.bilibili.com/video/BV12E411A7ZQ 注意:三引号只是把代码注释掉了

一、基本输入输出

#-*- coding = utf-8 -*- #@Time : 2020/9/2 19:49 #@Author : Vivian #@File : demo1.py #@Sofeware : PyCharm #b站p4 p5 ''' print('标准化输出字符串') a=10 print(a) print("这是变量:", a) ''' ''' #输出 #格式化%d %s age=18 print("我的年纪是:%d 岁"%age) print("我的名字是%s,我的国籍是%s"%("vivian","中国")) print("aaa","bbb","ccc") print("www","baidu","com",sep=".") #sep为分隔符 print("hello",end="") print("hello",end="\t") #输出完hello后加tab print("hello",end="\n") #输出完hello后换行 print("hello\nworld") ''' ''' #输入 password=input("请输入密码") print("你刚刚输入的密码是:",password) ''' ''' a=10 b="abc" print(type(a)) print(type(b)) ''' ''' #强制类型转换 a=int("123") print(type(a)) b=a+100 print(b) '''

二、条件判断语句(if)

''' #条件表达式(判断语句)非0值和非空值都是True 注意缩进 if 0: print("True") else: print("False") ''' ''' score=77 if score>=90 and score<=100: print("A") elif score>=80 and score<90: print("B") elif score>=70 and score<80: print("C") else: print("F") ''' ''' import random #引入随机库 x=random.randint(0,2) print(x) '''

三、课后练习:猜拳游戏 代码:

#课堂练习 import random int1=int(input("请输入:剪刀(0)、石头(1)、布(2)")) int2=random.randint(0,2) if int1==0 and int2==0: print("你的输入为:剪刀(0)\n随机生成数字为:",int2,"哈哈,打平了") elif int1==0 and int2==1: print("你的输入为:剪刀(0)\n随机生成数字为:",int2,"哈哈,你输了") elif int1==0 and int2==2: print("你的输入为:剪刀(0)\n随机生成数字为:",int2,"哈哈,你赢了") elif int1==1 and int2==0: print("你的输入为:石头(1)\n随机生成数字为:",int2,"哈哈,你赢了") elif int1==1 and int2==1: print("你的输入为:石头(1)\n随机生成数字为:",int2,"哈哈,打平了") elif int1==1 and int2==2: print("你的输入为:石头(1)\n随机生成数字为:",int2,"哈哈,你输了") elif int1==2 and int2==0: print("你的输入为:布(2)\n随机生成数字为:",int2,"哈哈,你输了") elif int1==2 and int2==1: print("你的输入为:布(2)\n随机生成数字为:",int2,"哈哈,你赢了") elif int1==2 and int2==2: print("你的输入为:布(2)\n随机生成数字为:",int2,"哈哈,打平了") else: print("please enter the correct number")
最新回复(0)