第一周——入门与基础
1.1 开发工具
Pycharm具备开发效率高,能够分步骤验证程序,从写脚本上升到写代码。
Python 建议使用3.0以上的版本,2开头的版本会在2020年正式淘汰,所以希望各位使用2系列的朋友尽快适应3系Python。
1.2 变量
定义规则: 变量名只可为字母,数字或下划线的任意组合.
第一个字符不可为数字.
部分已经被Python定义的关键词不可声明为变量名.
e.g. 定义变量 name = "Tianwei Bai"
print("my name is",name)
输出 my name is Tianwei Bai
1.3 用户交互程序
用户交互程序即为把赋值的任务交给用户。
e.g. username = input("username:")
password = input("password:")
print(username,password)
1.4 if else 流程判断
最基本的if else功能示范 结合上面的用户交互程序
e.g.简单的用户名密码设置以及登录程序
_username = 'TIANWEI BAI'
_password = 'weishao2608'
username = input('username:')
password = input('password:')
if _username == username and _password == password:
print("welcome",username)
else: print("wrong username or password,please try again.")
1.5第一周小结 综合示范程序(引入简单的break)
猜年龄
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#break跳出本循环 continue跳出本次循环直接进入下一次循环
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count +=1
if count == 3:
countine_confirm = input("do you want to keep guessing..?")
if countine_confirm != 'n':
count =0
升级版猜年龄:
情景:为少的好基友饭饭刚刚度过了23岁生日,不过读者朋友们大家不知道,为少同学想做一个程序来让读者朋友们猜一猜饭饭的年纪。
一回合有五次机会,猜大猜小都会有提示,如果猜对了直接给出结果,如果五次机会用完给用户一个选择是否继续。
Fanfan_age = 23
count = 0
while count < 5:
guess_age = int(input("guess_age:"))
if guess_age == Fanfan_age:
print("Congrets,you are right!")
break
elif guess_age < Fanfan_age:
print("Think bigger please")
else :
print("Think smaller please")
count +=1
if count ==5:
second_round_asking = input("The 5 changes have run out, do you want to get 5 more changes?Y or N")
if second_round_asking == 'Y':
count = 0
else:
print("Sorry, game over!")
break

浙公网安备 33010602011771号