ptyhon基础篇 day1
Posted 不要被骄傲遮蔽了双眼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ptyhon基础篇 day1相关的知识,希望对你有一定的参考价值。
1.变量
print(‘helloworld!‘) name = ‘alex‘ name2 = ‘jack‘ print(name,name2)
2.input
#用户输入 username = input(‘username: ‘) print(type(username)) age = int(input(‘age: ‘)) print(type(age)) job = input(‘job: ‘) salary = input(‘salary: ‘)
3.字符串
#格式化字符串 info = ‘‘‘ ------------info---------- username:%s age:%s job:%s salary:%s ‘‘‘%(username,age,job,salary) print(info)
4.用户登录
#密码隐藏
import getpass username = input(‘username:‘) pwd = getpass.getpass((‘password:‘)) print(type(pwd)) print(username,pwd)
#用户登录验证
username = ‘alex‘ password = ‘123‘ _username = input(‘username: ‘).strip() _password = input(‘password: ‘) if username == _username and password == _password: print(‘%s login ...‘%username) else: print(‘error..‘)
5, while .猜年龄
#单次猜年龄,str() > int() old_of_alex = ‘33‘ guess_old = input(‘your guess old : ‘) old_of_alex1 = 33 guess_old1 = int(input(‘your guess old : ‘)) if guess_old == old_of_alex : print(‘you are right‘) elif guess_old > old_of_alex: print(‘it is too bigger‘) elif guess_old < old_of_alex: print(‘it is too smaller‘)
#无限次 猜年龄
old_of_alex = 33 while True: guess_old = input(‘your guess old : ‘).strip() if guess_old.isnumeric(): guess_old = int(guess_old) if guess_old == old_of_alex : print(‘you are right‘) break elif guess_old > old_of_alex: print(‘it is too bigger‘) elif guess_old < old_of_alex: print(‘it is too smaller‘) else: print(‘error... please enter number‘)
#3次 猜年龄
old_of_alex = 33 count = 0 while True: guess_old = input(‘your guess old : ‘).strip() count +=1 if count <= 3: if guess_old.isnumeric(): guess_old = int(guess_old) if guess_old == old_of_alex : print(‘you are right‘) break elif guess_old > old_of_alex: print(‘it is too bigger‘) else: print(‘it is too smaller‘) else: print(‘error... please enter number‘) else: print(‘fuck off‘) break
#3次 猜年龄 while count < 3: old_of_alex = 33 count = 0 while count < 3: guess_old = input(‘your guess old : ‘).strip() count +=1 if guess_old.isnumeric(): guess_old = int(guess_old) if guess_old == old_of_alex : print(‘you are right‘) break elif guess_old > old_of_alex: print(‘it is too bigger‘) else: print(‘it is too smaller‘) else: print(‘error... please enter number‘) print(‘fuck off‘)
#3次 猜年龄 while count < 3: # else:print(‘fuck off‘) old_of_alex = 33 count = 0 while count < 3: guess_old = input(‘your guess old : ‘).strip() count +=1 if guess_old.isnumeric(): guess_old = int(guess_old) if guess_old == old_of_alex : print(‘you are right‘) break elif guess_old > old_of_alex: print(‘it is too bigger‘) else: print(‘it is too smaller‘) else: print(‘error... please enter number‘) else: print(‘fuck off‘)
6.continue break
#while中contine , break 的区别
#测试1 count = 0 while True: print(‘count : ‘,count) count +=1 if count == 10000: continue #while是死循环,跳出本次循环,会继续循环下一次,死循环 #测试2 count1 = 0 while True: print(‘count1 : ‘,count1) count1 +=1 if count1 > 19000: break # 跳出循环,终止距离break最近的那个循环,while
#for 循环中continue和break效果相同 count = 0 for i in range(10): if count == 5: continue #跳出本次循环,count = 5 跳出for循环,终止循环下去 print(count) count +=1 print(‘\n-----\n‘) count1 = 0 for i1 in range(10): if count1 == 5: break print(count1) count1 +=1
7.for循环
#for 循环 for i in range(10): print(‘loop‘,i)
#for (start,stop,step) for i in range(0,100,3): print(i)
# for 循环实现 3次 猜年龄
old_of_alex = 33 for count in range(3): guess_old = int(input(‘your guess old : ‘).strip()) if guess_old == old_of_alex : print(‘you are right‘) break elif guess_old > old_of_alex: print(‘it is too bigger‘) else: print(‘it is too smaller‘) print(‘too times ... fuck off‘)
# 猜年龄 可以 一直询问 old_of_alex = 33 count = 0 while True: guess_old = input(‘your guess old : ‘).strip() count +=1 if guess_old.isnumeric(): guess_old = int(guess_old) if guess_old == old_of_alex : print(‘you are right‘) break elif guess_old > old_of_alex: print(‘it is too bigger‘) else: print(‘it is too smaller‘) else: print(‘error... please enter number‘) if count == 3: choice = input(‘do you want to again [y/n]? > ‘).strip() if choice == ‘y‘: count = 0 elif choice == ‘n‘: break
8.嵌套
#循环嵌套 for i in range(10): print(‘-----‘,i) for j in range(10): print(i,j)
9.整型 长整型
#整形,长整型 ‘‘‘ Python2.x中有int long Python3.x中没有长整型概念了! >>> type(2**10000) <class ‘int‘> ‘‘‘
10 .三目运算符
#三元运算符 ‘‘‘ >>> a,b = 1,3 >>> max = a if a>b else b >>> max 3 ‘‘‘
作业:三次锁定
‘‘‘ 编写用户登录接口
1.输入用户密码
2.认证成功显示欢迎信息
3.输错3次锁定 ‘‘‘
username = ‘alex‘ password = ‘123‘ count = 0 while count < 3: _username = input(‘USERNAME : ‘).strip() _password = input(‘PASSWORD : ‘).strip() count += 1 if username == _username and password == _password: print(‘‘‘ ------------------- welcome the VIP %s ------------------- ‘‘‘%(username)) break else: print(‘用户名或密码错误,请重新输入\n‘) else: print(‘超过3次,锁定登录‘)
以上是关于ptyhon基础篇 day1的主要内容,如果未能解决你的问题,请参考以下文章
100天精通智能网联汽车技术(基础篇)— Day1:自动驾驶技术概述