首页 > 代码库 > pyhton随笔02

pyhton随笔02

一、如何实现输入密码时不可见?

import getpass

pwd= getpass.getpass(“请输入密码:")

(pycharm内可能不可用)

 

二、if.....else

age=25

guess_age = int(input(‘你猜我多少岁?‘)) #python3.X 中input的是一个str,需要int()

if guess_age == age:

  print(‘Yes,you got it!‘)

elif guess_age > age:

  print(‘think smaller‘)

else :

  print(‘think bigger‘)

 

三、while

count = 0

while True:

  print(‘count:’,count)

  count = count +1

#guess age

age=25

count = 0

while count < 3:

  guess_age = int(input(‘你猜我多少岁?‘))

  if guess_age == age:

    print(‘Yes,you got it!‘)

  elif guess_age > age:

    print(‘think smaller‘)

  else :

    print(‘think bigger‘)

  count +=1

  if count ==3:

    continue_confirm = input(‘Do you want to continue?‘:)

      if continue_confirm != "n":

        count = 0

#else:

#  print(‘you have try too many times‘)

 

三、for循环

for i range(10):

  print(”loop:“,i)

 

for i range(0,10,3):#从0开始,到10结束,步长为3

  print(”loop:“,i)

 

#guess age

age=25

count = 0

for i in range(3):

  guess_age = int(input(‘你猜我多少岁?‘))

  if guess_age == age:

    print(‘Yes,you got it!‘)

  elif guess_age > age:

    print(‘think smaller‘)

  else :

    print(‘think bigger‘)

  count +=1

else:

  print(‘you have try too many times‘)

 

#! usr/bin/env python
# encoding:utf-8
# __author__="Macal"
‘‘‘
for i in range(0,10):
if i < 5:
print("loop",i)
else:
continue
print(‘hehe‘)
‘‘‘
for i in range(10):
print(‘----------‘, i)
for j in range(10):
print(j)
if j > 5:
break #结束当前循环

 

pyhton随笔02