王二学习python的笔记以及纪录,如有雷同,那也没事,有想一起交流的可以的评论区留下你的联系方式
python2 源码不标准,混乱,重复代码太多,
python3 统一 标准,去除重复代码。
编译型:一次性将所有程序编译成二进制文件。
缺点:开发效率低,不能跨平台。
优点:运行速度快。
:C,C++等等。
解释型:当程序执行时,一行一行的解释。
优点:开发效率高,可以跨平台。
缺点:运行速度慢。
:python ,php,等等。
如下是我自己的练习以及思考,方便起见,所有的思考都放在代码中,
1. 做一个猜年龄的游戏
age_of_boy = 18 guss = int(input(‘>>:‘)) flag = True while flag ==True: if guss > age_of_boy : print(‘Too large, please try again‘) guss = int(input(‘>>:‘)) elif guss < age_of_boy : print(‘Too small, please try again‘) guss = int(input(‘>>:‘)) else: print(‘bingo, age of boy is: ‘,age_of_boy) print(‘bingo, age of boy is: %d‘ %(age_of_boy)) flag = False ‘‘‘ 心得:尝试连接非字符串值与字符串(导致 “TypeError: Can‘t convert ‘int‘ object to str implicitly”) print(‘bingo, age of boy is: ‘ + age_of_boy) 字符串与整型不能连接,直接相继打印即可 或者占空 ‘‘‘
2. 屏幕显示从0-100
#1 ‘‘‘ count = 0 while count<=100: print(count) count = count + 1 ‘‘‘ #2 count = 0 flag = True while flag == True: print(count) count = count + 1 if count >100: flag = False
3.从1加到100
count = 1 sum = 0 while count <= 100: sum=sum + count count = count + 1 print(‘1+2+3...100=‘,sum)
4.求1-2+3-4+5 ... 99的所有数的和
# 如何确定数字前的符号 #第一反应 奇数 偶数 分离 count = 0 sum = 0 while count < 100: count = count + 1 if count % 2 == 1 : sum = sum + count if count % 2 == 0 : sum = sum - count print(sum,count) ‘‘‘ # 网友答案 i = 0 sum1 = 0 sum2 = 0 while i<100: i = i + 1 num1 = i%2 if num1 == 1: sum1 = sum1 + i else: sum2 = sum2 + i print(sum1-sum2) ‘‘‘ # 思路相同,但是我的少定义一个变量
5.输出 1-100 内的所有偶数,奇数
#如何判断奇偶数 除2取余 print(‘输出1-100的奇数‘) count = 0 while count < 100: count = count + 1 if count % 2 == 1: print(count) print(‘输出1-100的偶数‘) count = 0 while count < 100: count = count + 1 if count % 2 == 0: print(count) # 心得:while if 记得写: 记得写空格, 规范代码
6. 显示1-5,90-100
‘‘‘ # 自己写 count = 1 while count <=100 : if count <= 5 or count >= 90 : print(count) count = count + 1 ‘‘‘ ‘‘‘ #使用continue count = 0 while count <= 100: count = count + 1 if count > 5 and count < 90 : continue print(count) ‘‘‘ # break语句 练习 count = 0 while count <=100 : count = count + 1 if count > 5 : break print(count) while count <=100 : count = count + 1 if count < 90 : continue print(count)
7.用户登录(三次机会)
#涉及人机交互 跟猜年龄相似 ‘‘‘ answer = 12306 count = 3 guess = int(input(‘请输入你的答案,剩余机会3次:‘)) while count >= 1: count = count - 1 if guess == answer : print(‘正在登陆,请稍后‘) break else: if count == 0: print(‘机会用完啦,明天再来吧‘) break print(‘密码错误,剩余次数:‘,count) guess = int(input(‘请输入你的答案:‘)) continue ‘‘‘ # 根据网友答案的反思 没有账号 account = ‘NBA‘ answer = 12306 count = 3 while count >= 1: count = count - 1 acc = input(‘请输入账号‘) if acc == account: count = 3 while count >= 1: count = count - 1 ans = int(input(‘请输入密码‘)) if ans == answer : print(‘正在登陆请稍后‘) quit() else : print (‘密码错误,剩余次数‘,count) continue else: print(‘账号输入错误,剩余次数‘,count) if count == 0: print(‘机会用完啦,明天再来吧‘) break continue # break 只能跳出本次循环,quit()退出执行程序 ‘‘‘ #网友答案 account="wangning" password=123456 i = 0 while i<3: i = i + 1 acc=input("please input your account:") if acc == account: i = 0 while i<3: i = i + 1 passwd=int(input("please input your password:")) if passwd == password: quit() else: print("the password is error") continue else: print("the account is error") continue ‘‘‘
原文:https://www.cnblogs.com/wan2-0/p/10530047.html