#变量赋值的关系
name="Eleanor"
name2=name
print(name,name2)
name="MM"
print(name,name2)
name="Eleanor"
age=26
province="Hei Longjiang"
school="Northeast Agriculture University"
son_of_twins_brother_gf="Fang Fang"
SonOfTwins="Da Wu & Xiao Wu"
print(name,age,province,school)
#每行最多不超过80个字符
ret=map(lambda x:x**x, range(10)) #this cold to mutiply.....x
#用户输入的写法,以及将其打印出来的写法
user_input= input("input your name:")
print("user input msg:", user_input)
#输入姓名、年龄和工作,然后统一打印出来
name= input("input your name:")
age= int(input("input your age:")) #int是强制将字符串转换为数值,convert str to int
job= input("input your job:")
msg= "
Information of user %s
------------------------
Name: %s
Age : %d #%f %s
Job : %s
----------End-----------
" %(name,name,age,job)
print(msg)
#input的语句默认接收的是字符串
#%s代表引入字符串;%d表示引入数值型;%f表示带小数点
#import是导入模块的作用
import getpass # 密文输入的语句
username=input("username:")
password=getpass.getpass("password:")
print(username,password)
#os模块
import os
os.system('df -f') #执行命令
os.mkdir('yourDir') #创建目录
cmd_res=os.popen("df -h").read() #保存命令的结果
import sys #查找模块的存储位置
print(sys.path) #自己写的模块一般放的位置'/userlib/python2.7/dist-package'
#if 程序
user="Eleanor"
passwd="Eleanor921"
username=input("username:")
password=input("password:")
if user==username:
print("username is correct...")
if password==passwd:
print("Welcome login...")
else:
print("password is invalid")
else:
print("用户名输入错误")
----if 程序优化版----
user="Eleanor"
passwd="Eleanor921"
username=input("username:")
password=input("password:")
if user==username and passwd==password:
print("Welcome login...")
else:
print("Invalid username or passwod...")
#guess age
age=27
guess_num=int(input("input your guess num:"))
if guess_num==age:
print("Congratulations! you got it.")
elif guess_num>age:
print("Think smaller!")
else:
print("Think bigger!")
--------------------------------------------------------------------
#guess age v2
'''
for i in range(10): #for语句写法
print(i)
'''
age=27
for i in range(10):
if i<3:
guess_num=int(input("input your guess num:"))
if guess_num==age:
print("Congratulations! you got it.")
break #不往后走了,跳出整个loop
elif guess_num>age:
print("Think smaller!")
else:
print("Think bigger!")
else:
print("Sorry,too many times.Bye...")
break
--------------------------------------------------------------------
#guess age v3
age = 27
for i in range(10):
#i = 0
print("new i2", i) #对i进行计数
if i < 3:
guess_num = int(input("input your guess num:"))
if guess_num == age:
print("Congratulations! you got it.")
break #不往后走了,跳出整个loop
elif guess_num > age:
print("Think smaller!")
else:
print("Think bigger!")
else:
# print("Sorry,too many times.Bye...")
#break
continue_confirm = input("Do you want to continue:")
if continue_confirm == 'y':
#pass #
i = 0
print("new i",i)
else:
print("OK! Bye")
break
--------------------------------------------------------------------
#guess age v4
age = 27
counter = 0 #在循环外定义计数器
for i in range(10):
print('-->counter:',counter)
if counter < 3:
guess_num = int(input("input your guess num:"))
if guess_num == age:
print("Congratulations! you got it.")
break #不往后走了,跳出整个loop
elif guess_num > age:
print("Think smaller!")
else:
print("Think bigger!")
else:
continue_confirm = input("Do you want to continue:")
if continue_confirm == 'y':
counter = 0
continue #跳出当次循环
else:
print("OK! Bye")
break
counter += 1 #counter = counter + 1
--------------------------------------------------------------------
--------------------------------------------------------------------