学习input(),while循环和函数
文章目录
input()工作原理
>>> name=input("tell me your name:")
tell me your name:jack
>>> print(name)
jack
>>> name=input("tell me your name:")
tell me your name:jack
>>> print("ok,hello "+name)
ok,hello jack
>>>
>>> want="i want to know your name."
>>> want += "\nplease enter your name:"
>>> name=input(want)
i want to know your name.
please enter your name:jack
>>> print("i know your name is "+name)
i know your name is jack
>>>
while循环
输出1~5
num=1
while num<6:
print(num)
num+=1
------------------
1
2
3
4
5
让用户选择何时退出
message="you are in game,please enter a word,i will back it to you"
message+="\nif you want to exit please enter 'quit':"
word=''
while word !="quit":
word=input(message)
print(word)
-----------------------
you are in game,please enter a word,i will back it to you
if you want to exit please enter 'quit':hello
hello
you are in game,please enter a word,i will back it to you
if you want to exit please enter 'quit':how are you?
how are you?
you are in game,please enter a word,i will back it to you
if you want to exit please enter 'quit':quit
quit
>>>
这个程序很好,唯一的缺点是它将‘quit’也打印了出来,要想解决这个问题,加一个if语句就可以了
message="you are in game,please enter a word,i will back it to you"
message+="\nif you want to exit please enter 'quit':"
word=''
while word !="quit":
word=input(message)
if word !='quit':
print(word)
-------------------
you are in game,please enter a word,i will back it to you
if you want to exit please enter 'quit':today is a good day
today is a good day
you are in game,please enter a word,i will back it to you
if you want to exit please enter 'quit':where are you?
where are you?
you are in game,please enter a word,i will back it to you
if you want to exit please enter 'quit':quit
>>>
break
n=1
while n<11:
print(n)
n+=1
if n==5:
break
-----------------
1
2
3
4
>>>
continue
n=0
while n<10:
n+=1
if n%2==0:
continue
else:
print(n)
---------
1
3
5
7
9
>>>
验证列表
people=['steve','tom','tack']
students=[]
while people:
s=people.pop()
print("people:"+s)
students.append(s)
print("these people are students:")
for student in students:
print(student.title())
--------------------------
people:tack
people:tom
people:steve
these people are students:
Tack
Tom
Steve
>>>
删除包含特定值的所有列表元素
nums=['1','1','2','1','1','1','5']
print(nums)
while '1' in nums:
nums.remove('1')
print(nums)
--------------------
['1', '1', '2', '1', '1', '1', '5']
['2', '5']
>>>
使用用户输入来填充字典
感觉这个就很有趣,做个调查问卷吧~~
responses={}
active=True
while active:
name=input("请输入你的姓名:")
response=input("你喜欢上网课吗?(喜欢/不喜欢)")
responses[name]=response
next=input("你希望让下一个人填写问卷吗?(yes/no)")
if next =='no':
active=False
print("\n----Results----")
for name,response in responses.items():
print(name+""+response+"上网课")
--------------------------------
请输入你的姓名:steve
你喜欢上网课吗?(喜欢/不喜欢)喜欢
你希望让下一个人填写问卷吗?(yes/no)yes
请输入你的姓名:tom
你喜欢上网课吗?(喜欢/不喜欢)不喜欢
你希望让下一个人填写问卷吗?(yes/no)no
----Results----
steve喜欢上网课
tom不喜欢上网课
>>>
你也可以换成其他问题
函数
一个简单的函数
def hello():
print("hello world")
hello()
------------
hello world
向函数传递信息
def hello(name):
print("How are you,"+name.title()+'?')
hello('tom')
------------
How are you,Tom?
实参和形参
在上面的例子中,变量name是一个形参。在代码hello(‘tom’)中’tom’是一个实参,将实参‘tom’传递给了函数hello(),这个值被存储在形参name中。
位置实参
def flowers(flowers_type,flowers_color):
print("\nI like "+flowers_type)
print("It is "+flowers_color)
flowers('rose','red')
-------------
I like rose
It is red
多次调用
def flowers(flowers_type,flowers_color):
print("\nI like "+flowers_type)
print("It is "+flowers_color)
flowers('rose','red')
flowers('peony','pink')
---------------------
I like rose
It is red
I like peony
It is pink
实参位置顺序很重要,顺序不对,意思就错了。
这样调用也是可以的
flowers(flowers_type='peony',flowers_color='pink')
你也可以固定值
def flowers(flowers_type,flowers_color='pink'):
print("\nI like "+flowers_type)
print("It is "+flowers_color)
flowers('rose')
flowers(flowers_type='peony',flowers_color='red')
---------------
I like rose
It is pink
I like peony
It is red
简单返回值
def name(first_name,last_name):
name=first_name+' '+last_name
return name.title()
full_name=name('xing','weikun')
print(full_name)
------------------
Xing Weikun
实参可选
def name(first_name,last_name,middle_name=''):
if middle_name:
name=first_name+' '+middle_name+' '+last_name
else:
name=first_name+' '+last_name
return name.title()
full_name=name('xing','weikun')
print(full_name)
full_name=name('jack','bc','white')
print(full_name)
---------
Xing Weikun
Jack White Bc