Python学习笔记--01

本文通过示例介绍了Python的基础语法,包括变量赋值、条件判断、循环结构等,并演示了如何使用Python进行用户输入处理及简单的猜数字游戏。

#变量赋值的关系
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
--------------------------------------------------------------------
--------------------------------------------------------------------

转载于:https://www.cnblogs.com/EleanorInHarbin/p/8261058.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值