python
while循环
while 语句:
执行语句
结束条件
#应用while输出1到11 counts = 1 while True: print("counts:", counts) counts = counts +1 #counts +=1 if counts == 11: break
for 循环
for 变量 in range(起始值,终止值,步长):
循环语句
#从零开始以3为步长不超过10 for i in range(0,10,3) print(i)
python注释中获得输入信息的几种方式
方法1:
变量1 = input(“A”)
变量2 = input(“B”)
info = '''
C:%s
D:%s
'''%(变量1,变量2)
#输入姓名、年龄,输出姓名、年龄 name = input("name") age = int(input("age")) info = ''' -------------info of %s--------- name:%s age:%d '''%(name,name,age) print(info)
方法2:
变量1 = input(“A”)
变量2 = input(“B”)
info = '''
C:{E}
D:{F}
'''.format(E=变量1,F=变量2)
info2 = ''' -------------info of {_name}--------- name:{_name} age:{_age} '''.format(_name=name, _age=age) print(info2)
方法3:
变量1 = input(“A”)
变量2 = input(“B”)
info = '''
C:{0}
D:{1}
'''.format(变量1,变量2)
info3 = ''' -------------info of {0}--------- name:{0} age:{1} '''.format(name, age) print(info3)
简单登陆
#引入一个getpass import getpass #定义一个用户名和密码 _name = '123' _password = '456' #获得键盘输入值 name = input("name") # password = getpass.getpass("password") # 密码密文,pycharm不好使 password = input("password") #判断用户名和密码是否一致 if _name == name and _password == password: #将用户名传给输出语句中 print("welcome user{name} login...".format(name = name)) print("----------------") else: print("Invalid password")
猜大小小程序
number = 20 #for 循环 range(开始,结束,步长) for i in range(1,10,2): #获得输入值 guess_number = int(input("guess number:")) #进行比较 if guess_number == number: print("yes, you got it.") elif guess_number > number: print("think smaller ...") else: print("think bigger!") print("loop",i)