Python day1

# ascii 只能显示英文,特殊字符和数字
# 万国码:unicode 最开始为16位,之后为32字节
# 升级版:utf-8 utf-16 utf-32
# utf-8: 最少用一个字节,一个字节表示一个英文
# 欧洲16位,两个字节
# 亚洲24位 三个字节
# gbk 国产编译器。只能用于中文和ascii码中的文字

注释

'''
#多行注释(这位单行注释)
print('你好世界')
'''
View Code

变量的赋值

1 name1 = '123'
2 name2 = "02"
3 name3 = name2
4 
5 print(name1, name2, name3)
View Code

python中常量表示的方法

1 #将变量名设置全部大写
2 AGE = 20
View Code

 

在Python3里不再有long类型了,全都是int

在Python中,加了引号的字符都被认为是字符串!

用户交互

1 name = input('请输入用户名: ')
2 print(name, type(name))
View Code

input接收的所有输入默认都是字符串格式!

把str转成int

1 age = int(  input("Age:")  )
View Code

pratice

1 name = input('what is your name\n')
2 age = input('how old are you\n')
3 hometown = input('where are you from\n')
4 
5 print(name + '\n' + age + "\n" + hometown)
6 #作用就是复制自己多少次,在拼接在一起
7 #只能字符串和字符串拼接
8 print(name * 10)

格式化输出练习

 1 print('------------ info of Alex Li -----------')
 2 name = input('what is your name\n')
 3 age = input('how old are you\n')
 4 job = input('what is your job\n')
 5 hobby = input('what is your hobby\n')
 6 
 7 print('Name     : ' + name)
 8 print('Age      : ' + age)
 9 print('Job      : ' + job)
10 print('hobby    : ' + hobby)
11 print('------------- end -----------------')

if else 语句

1 age = 20
2 if age > 18:
3     print("You are a adult.")
4 else:
5     print("You are a child.")

条件语句的运用

 1 num = 20
 2 GuessNum = int(input('Please Enter the number you guess:'))
 3 
 4 if GuessNum > 20:
 5     print('Big')
 6 elif GuessNum == 20:
 7     print('Bigon')
 8 else:
 9     print('small')
10 
11 grade = int(input('Please input a grade' + '(\n I will give your level): '))
12 
13 if grade > 100:
14     print('Error input')
15 elif grade > 90:
16     print('A')
17 elif grade > 80:
18     print('B')
19 elif grade > 70:
20     print('C')
21 elif grade > 60:
22     print('D')
23 else:
24     print('E')

#while else 的使用
#while 后面的else 作用是指,
#当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

 1 count = 0
 2 while count <= 5:
 3     count += 1
 4     print(count)
 5 
 6 else:
 7     print('循环结束')
 8 
 9 count = 0
10 while count <= 5:
11     count += 1
12     print(count)
13     break
14 else:
15     print('循环结束')

#pratice 1
#1、使用while循环输入 1 2 3 4 5 6 8 9 10

1 i = 0
2 while i < 10:
3     i = i + 1
4     print(i)
5     if i == 7:
6         continue

#pratice 2
#求1-100的所有数的奇数和

1 i = 0
2 sum = 0
3 while i < 100:
4     i = i + 1
5     sum += i
6 print(sum)

#pratice 3
#求1-100的所有数的奇数和

1 i = 0
2 sum = 0
3 while i < 100:
4     i = i + 1
5     if i % 2 != 0 or i == 1:
6         sum += i
7     else:
8         continue
9 print(sum)

#pratice 4
#求1-100的所有数的偶数和

1 i = 0
2 sum = 0
3 while i < 100:
4     i = i + 1
5     if i % 2 == 0 and i != 1:
6         sum += i
7     else:
8         continue
9 print(sum)

#pratice 5
#求1-2+3-4+5 ... 99的所有数的和

i = 0
sum = 0
while i < 99:
    i += 1
    if i % 2 == 0 and i != 1:
        sum -= i
    else:
        sum += i
print(sum)

#practice 6
#用户登入

 1 i = 0
 2 username = 'admin'
 3 password = 'admin123'
 4 
 5 while i < 3:
 6     pw = input('请输入密码:')
 7     if pw == password:
 8         print('登入成功')
 9         break
10     else:
11         print("输入有误,请重新输入")
12         i += 1
13         continue
14 if i == 3:
15     print("用户已被锁定")

 

# 计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和
 1 # i = 0
 2 # sum = 0
 3 # while i < 99:
 4 #     i += 1
 5 #     if i == 88:
 6 #         continue
 7 #     elif i % 2 != 0:
 8 #         sum += i
 9 #     else:
10 #         sum -= i
11 #
12 # print(sum)

 


# 计算 1 - 2 + 3 ... -99 中除了88意外所有数的总和

 1  = 0
 2 j = -1
 3 sum = 0
 4 while i < 99:
 5     i += 1
 6     if i == 88:
 7         continue
 8     else:
 9         j *= -1
10         sum += i * j
11 print(sum)

 

 
# ⽤户登陆(三次输错机会)
# 且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
 1 # ⽤户登陆(三次输错机会)
 2 # 且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
 3 un = 0  # 记录帐号输错的次数
 4 up = 0  # 记录密码输错的次数
 5 state = True    # 程序当前状态
 6 username = 'admin'
 7 password = '123456'
 8 
 9 while state:
10     input_username = input("Please input your username(you still have %d times): "%(3 - un))
11     if input_username == username:
12         input_password = input("Please input your password(you still have %d times): " %(3 - up))
13         if input_password == password:
14             print('Loading successfully.')
15         else:
16             up += 1
17             if up == 3:
18                 print("Your ID was locked.")
19                 break
20             else:
21                 print("Your password is error, please check it then input again: ")
22     else:
23         un += 1
24         if un == 3:
25             print("Your ID was locked.")
26             break
27         else:
28             print("Your username is error, please check it then input again: ")

 

 
posted @ 2018-08-28 16:59  Rainm  阅读(199)  评论(0编辑  收藏  举报