求生之路Python W1

 

初学程序的第一天,总是从Hello World开始,Python也不例外;

1.尝试第一个Python程序print输出:

1 print("Hello World !")

 运行结果:

 

2.赋予变量与分行输出:

  • 使用print()作为信息输出;
  • 使用 xxx = yyy ,将yyy的值赋予xxx;
  • 使用input()语句获取用户输出信息,然后赋予变量(=号左边),使用int()语句限制输入类型,int为整形,默认为字符串型(string)
  • 使用'''注释文本并格式化,最后调用print()输出。
 1 #登记学生资料
 2 print("请输入以下学生信息:")
 3 s_id = int(input("学号 :"))
 4 s_name = input("姓名 :")
 5 s_age = int(input("年龄 :"))
 6 s_class = input("班级 :")
 7 
 8 #录入以上登记信息并输出学生资料卡
 9 stu_info ='''
10 ------------学生信息卡:{s_name}-----------
11 
12 学号: {s_id}
13 姓名: {s_name}
14 年龄: {s_age}
15 班级: {s_class}
16 
17 ---------------------------------------
18 '''.format(s_id=s_id,s_name=s_name,s_age=s_age,s_class=s_class)
19 
20 print(stu_info)
21 print(s_name,"的资料录入完成!")

 运行结果:

 

3.库的作用,标准库和第三方库,Python有一些内置的标准库,比如getpass

 1 #导入库getpass
 2 import getpass
 3 
 4 #赋值,预置
 5 username = 'lonyi'
 6 password = 'lonyi@123'
 7 
 8 #获取用户输入,getpass将用户输入字符转化成密文
 9 _username = input("username : ")
10 _password = getpass.getpass("password : ")
11 
12 #对用户输入和预置值进行判断
13 if _username == username and _password == password:
14     print("Welcome user {name} login ....".format(name=username) )
15 else:
16     print("Invalid username or password !")

运行结果:

 

4.循环语句,有for型、if型和while型。用于处理重复操作的任务,循环的执行一些语句,可以是单语句也可以是多条件语句,配合的else,elif使用。

  while语句比较重要的两个命令:

 

  • continue,跳过本次循环,即不输出;
  • break,退出循环;

 

简单的while循环:

1 count = 0
2 #while判断结果是否为真,假则跳出循环,即count大于等于5
3 while count < 5:
4     print("The count is:", count)
5     count = count + 1
6 
7 #跳出循环,打印Good bye!
8 print("Good bye!")

运行结果:

 

 实例:

——————————猜测岁数游戏
_需求:
  • 预设岁数值,使用循环语句进行判断;
  • 如果用户输入值比预设值大则提示数值过大,如果用户输入值比预设值小则提示数值过小;
  • 统计输入次数,并做出相应的监控提示,限制输入次数。
 1 #猜测岁数游戏
 2 
 3 #预置值
 4 age_lonyi=26
 5 
 6 #预设input_count为0,用于统计的用户输入的次数
 7 input_count=0
 8 #开始while循环
 9 while True:
10     if input_count == 5:
11         #当input_count次数达到5次,即跳出循环
12         break
13     #获取用户输入,定位为int类型
14     guess_age=int(input("Pls guess my age ? "))
15 
16     #开始if循环判断
17     if guess_age == age_lonyi :
18         print("Yeah,you so good. ")
19         #答对结束,即跳出循环
20         break
21     #多条件循环判断
22     elif guess_age > age_lonyi :
23         print("You're think amaller ...... again ! ")
24 
25     else:
26         print("You're think bigger! next")
27     #没执行一次输入,input_count值就+1
28     input_count +=1
29 
30 #新的if循环,开始判断上一个if循环结束后的input_count值结果,输出相应的提示。
31 if input_count ==5:
32     print("您已经猜错了",input_count, "次,请十分钟后重试!")
33 elif input_count > 1:
34     print("您已经猜错了",input_count, "次,下次注意了!")
35 else:
36     print("很好,已完成测试")

运行结果1:

 运行结果2:

 

 进阶版

  •  让用户自由选择结束还是仍然继续。
 1 #猜测岁数游戏,进阶版1
 2 
 3 age_lonyi=26
 4 
 5 input_count=0
 6 while input_count < 5:
 7 
 8     guess_age=int(input("guess age : "))
 9 
10     if guess_age == age_lonyi :
11         break
12 
13     elif guess_age > age_lonyi :
14         print("You're think smaller! Try again ")
15 
16     else:
17         print("You're think bigger! Try again")
18     input_count +=1
19 
20     #当input_count为5时,提示用户是否还继续,如果用户选择yes,则input_count重置为0
21     if input_count == 5:
22         #\线为转义符
23         try_1 = input("Did you want to try again ? Input \"yes\" or \"no\" ")
24         #判断用户输入的是否为no,非no为真
25         if try_1 != 'no':
26             print("You want to play again , OK ,Let's go !")
27             input_count = 0
28         else:
29             print("Okey,You choose the end code!")
30             #此时input_count不改变,为5,则while循环结束。
31 
32 if input_count == 5:
33     print("您已经猜错了",input_count, "次,已结束!")
34 elif input_count > 1:
35     print("您已经猜错了",input_count, "次,下次注意了!")
36 else:
37     print("很好,已完成猜测")

运行结果1:

运行结果2:

 

 

  • 的 的
posted @ 2017-11-19 13:03  RinCyan  阅读(158)  评论(0)    收藏  举报