day2-基础 变量,判断,循环


 1.第一个程序

 1 print ("Hello World!") 

  输出:

 1 Hello World 

2.第一个变量

 1 a = ("Hello World") 2 print (a) 

  输出:

 1 Hello World

 3.判断

  最简单判断

 1 if true: 2 print("Yes,is true") 

  输出:

 1 Yes,is true 

  猜年龄

1 age_of_my = 18              #定义一个变量为18
2 age = int(input("age:"))    #定义一个变量让用户输入
3                 
4 if age == age_of_my:     #如果age=18,则打印Yes,Bingo
5         print("Yes,Bingo")
6 elif age > age_of_my:    #如果age>18,则大运Think small
7         print("Think small")
8 else:              #如果以上都不是,则打印Think Big
9         print("Think Big")

  输出:

 1 age:12 2 Think Big 

  用户登录

1 import getpass                        #导入getpass模块
2 username = input("username:")                #定义变量让用户输入
3 password = getpass.getpass("password:")                #定义变量并使用getpass模块使密码加密,让用户输入
4 _username = 'faker'                         #定义变量为faker
5 _password = 'abc123'                     #定义变量为abc123
6 if username == _username and password == _password:    #如果_username=username和_password=password,则打印Welcom user faker
7 print("Welcom user {0}".format(username))      #否则打印Username or password error  8 else: 9 print("Username or password error")

4.输出格式化

 1 name = input("name:")
 2 age  = int(input ("age:"))
 3 position = input("position:")
 4 info = '''
 5 --------SKT T1 clan member information table--------
 6 name:%s                              #这里%s是一个临时变量,后面按顺序引用变量
 7 age:%s
 8 position:%s
 9 ''' % (name,age,position)
10 
11 info2 = '''
12 --------SKT T1 clan member information table--------
13 name:{_name}
14 age:{_age}
15 position:{_position}                    #这里{}中的东西是变量,后面引用外面的变量
16 '''.format(_name=name,
17            _age=age,
18            _position=position)
19 info3 = '''
20 --------SKT T1 clan member information table--------
21 name:{0}
22 age:{1}                            #这里{}中的东西是临时变量,后面引用外面的变量
23 position:{2}
24 '''.format(name,age,position)
25 print(info3)

 5.while循环

  猜年龄

 1 age_of_my = 18                      #定义变量
 2 frequency = 0                       #定义变量
 3 while frequency < 3:                  #当frequency<3,开始循环
 4     age = int(input("age:"))              #定义变量,让用户输入
 5     if age == age_of_my:                 #如果 age=age_of_my,则打印yes,Bingo...,并且打破循环
6 print ("Yes,Bingo...") 7 break 8 elif age > age_of_my: 9 print ("Think Big...")            #如果age>age_of_my,则打印Think Big... 10 else: 11 print ("Think Small...")           #否则打印Think Small... 12 frequency += 1                   #每次循环结束则frequency+1 13 if frequency == 3:                 #如果frequency=3,则定义一个用户输入变量want_to 14 want_to = input("Do you want to countine?")15 if want_to == 'y':               #如果want_to=y,则frequency归零 16 frequency = 0

6.for循环

 1 age_of_my = 18
 2 frequency = 0
 3 for i in range(3):             #取值i在3内,也就是从0-2
 4     age = int(input("age:"))        #其他参考上面
 5     if age == age_of_my:
 6         print("Yes,Bingo...")
 7         break
 8     elif age < age_of_my:
 9         print("Think Small...")
10     else:
11         print("Think Big...")    #
12     frequency += 1        
13 else:                        #超过三次循环,则打印You have tried too many times...
14     print("You have tried too many times...")

原文地址:https://www.cnblogs.com/wazy/p/7717653.html