学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
想系统/深入学习某技术知识点…
一个人摸索学习很难坚持,想组团高效学习…
想写博客但无从下手,急需写作干货注入能量…
热爱写作,愿意让自己成为更好的人…

🍈作者简介:大家好,我是我不叫内谁,渴望知识储备自己的一个菜狗

🍓本文目标:简单介绍用户输入与while循环

1.函数input()的工作原理

该函数可以让程序暂停运行,等待用户输入一些文本,从屏幕读取后,将获取到的信息存储在一个变量中。

2.示例

全国青少年信息素养大赛 Python编程挑战赛小学_python

 函数input()接受一个参数,即需要让用户明白应该输入什么。

name = input("please tell your name:")
 print("hello " + name + "!")

输出如下

全国青少年信息素养大赛 Python编程挑战赛小学_while循环_02

 3.使用int()获取数字输入

若使用input()获取数字输入

year = input("How old are you:")
 print("hello " + year + "!")
 age <=12


 

输出如下

全国青少年信息素养大赛 Python编程挑战赛小学_python_03

 由此可见,使用input()函数的过程中,该函数只是将其当作一个字符串,并未存储于变量中,针对这一点,使用int()函数可以很好的避免。

全国青少年信息素养大赛 Python编程挑战赛小学_字符串_04

 4.while循环

number = 1
while number <= 5:
        print(number)
number +=1

输出如图

全国青少年信息素养大赛 Python编程挑战赛小学_python_05

message = "Tell me the answer or you can enter 'quit' to quit."
answer = input("Please enter:")
while answer != "quit":
        print("Please tell me answer")
        if answer != "quit":
                continue
        else:
                break

在上述while循环中,continue与break分别负责程序的循环和终止。

continue 即在 if answer != "quit":条件成立时保持循环。

break 即在上述条件不成立的时候跳出循环。

5、例题

如果你是一名工程师,需要对打开密码库的人进行身份确认。

现有三人,分别是Rygn,Muya,Qakai,其中有一名是敌方间谍,妄图窃取我方机密,你只能通过数据确认谁是谁,现在通过名字、年龄、专属密码制作一个程序,敌方间谍年龄为18,专属密码为”Susan",间谍并不知道自己的密码与他人不同。

答案

list = ["Rygn","Muya","Qakai"]
while True:
     print("Hello,what's your name?")
     name = input("Please enter your name: ")
     if name not in list:
         print("Who are you?")
         continue
     print("Hello, " + name + " nice to meet you.\nHow old are you?")
     age = input("Please enter: ")
     age = int(age)
     print("What's your password:")
     password = input("Please enter: ")
     if password == "Susan":
         if age == 18:
             print("Dear, " + name + "I know you are the espionage!")
         
         else:
             print("Who are you!!???")
     else:
         print("This is the condential.")
     break


输出如下

全国青少年信息素养大赛 Python编程挑战赛小学_学习_06