Python 学习第一周

一、pycharm安装

https://www.cnblogs.com/dcpeng/p/9031405.html

二,代码联系

1、输入输出(明文):

  username = input("usrname:")

       password = input("password:")

  print(username,password)

2、输入输出(暗文):

 

import getpass ##输入的时候没有显示输入字符

 username = input("usrname:")

password = getpass.getpass("password:")
print(username,password)

 

3、验证输入的账号密码是否正确:

 

_username = 'alex'
_password = 'abc123'
username = input("username")
password = input('password:')
if _username == username and _password ==password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password!")

 4、猜年龄 if else 

age_of_oldboy = 56
guess_age = int(input("guess age:"))###输入必须为整数,不让报错
if guess_age == age_of_oldboy:
print("yes,you got it.")
elif guess_age > age_of_oldboy:
print("think smaller....")
else:
print("think bigger!")

5、while循环
count = 0
while True:
print("count:",count)
count = count+1
if count ==100:
break
输出0-99

count = 0
while True:
if count ==3:
break
guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:
print("yes,you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller....")
else:
print("think bigger!")
count+=1
6、优化后
age_of_oldboy = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller....")
else:
print("think bigger!")
count+=1
7、for 循环
for i in range(10):
print("loop",i)

for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller....")
else:
print("think bigger!")
else:
print("you have tried too many times ..")

8、有间隔的for循环
for i in range(0,10,3):##隔三个数
print("loop",i)

输出结果

loop 0
loop 3
loop 6
loop 9

 

9、用户控制

age_of_oldboy = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,you got it.")
break
elif guess_age > age_of_oldboy:
print("think smaller....")
else:
print("think bigger!")
count+=1

if count==3:####重点
continue_confirm = input("do you want to keep guessing?")
if continue_confirm !='n':
count =0
10、双层循环

for i in range(10):
print('--------',i)
for j in range(10):
print(j)
if j>5:
break
posted @ 2019-06-02 15:09  hapen  阅读(128)  评论(0编辑  收藏  举报