标签:
#用户交互
1 age_oldboy = 56 2 guess_age = int(input(">>:")) 3 if guess_age == age_oldboy: 4 print("bingo") 5 else: 6 print("you are stupid")
#while循环
"""a = int(input("kuan:")) b = int(input("gao:")) jishu1 = 1 while jishu1 <= b: #外层的while执行一次,则内层while jishu2 = 1 #要循环完一整次1-a while jishu2 <=a: print("#",end="") jishu2 +=1 jishu1 +=1 print() """ print("---------------------9×9乘法表----------------------") num = 9 a = 0 b = 0 while b < num: b += 1 while a < b: a +=1 print(a,"*",b,"=",a*b,end=" ") #end后面空格换成制表符(\t)后对齐 将a *b 装换成str则省去空格。 print() a = 0
#循环中break与continue
flag = True while flag: user_input = int(input(">>:")) if user_input == 50: print("right") flag = False #替换成break elif user_input > 50: print("think smaller") else : print("think bigger") print("end") # break是指跳出循环不执行while 而continue是指继续下个循环(continue下面的都不执行) --------------------------------------------------------------------------- """num = 1 while num < 10: num +=1 if num == 3: continue print(num) #结果 2456...11 else: print("this for else") #continue 不会打断while本身所以有“this is for else” 若是换成break则不会有这句话。 """
上图为没有进行对齐和去空格的操作,具体方法代码中注释有。
用户登录
1 #! /usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 Password = "caoge1ge" 4 name = input("your name is:") 5 age = int(input("your age is:")) #再转换成整形,input接收的都是字符串 6 password = input("type yourpassword:") 7 if Password == password: #判断 8 print("welcome,{Name}".format(Name=name)) #退格的重要性 TAB!=四个空格 官方建议四个空格 9 info = ‘‘‘ 10 用户名:%s 11 年龄:%d 12 密码:%s 13 ‘‘‘ %(name,age,password) #s是string d是整形 14 print(info) #格式化输出最好先用‘’这样再注释的时候用“ 15 # msg = ‘‘‘ #注释多行的快捷键是ctrl+/ 16 # mingzi:{_name} 17 # nianling:{_nianling} 18 # ‘‘‘.format(_name=name, 19 # _nianling=age) 20 # print(msg) 21 else:print("wrong password")
标签:
原文地址:http://www.cnblogs.com/khal-Cgg/p/5792710.html