def greet_user(username):
print("侬好呀,"+username.title()+"!")
greet_user('alway')
#输出
侬好呀,Alway!
def favorite_book(title,author):#author="Alway"第二个形参指定默认值,如果
#提供了实参就会忽略这个默认值
# print("One of my favorite book is "+title+".")
# print("It's writen by "+author+"")
#favorite_book(title="Enjoy",author="Frank")#关键字实参,顺序无关紧要
#favorite_book("Run","Andy")#位置实参
full_mes=title+" is writen by "+author
return full_mes#返回值
#调用返回值的函数时,需要提供一个变量,用于存储返回的值full_mes1
full_mes1=favorite_book("Alice in Wonderland","Bob")
print(full_mes1)
#输出
Alice in Wonderland is writen by Bob
def car(name,year,person):
car_mes=person +" have " + name +" for "+ year +" years"
return car_mes
while True:
print("\nWhat's your car's name")
print("(caution:enter 'q' at any time to quit )")
in_name=input("your name:")
if in_name=='q':
break
in_year=input("year is:")
if in_year=='q':
break
in_person=input("person:")
if in_person=='q':
break
full_mes=car(in_name,in_year,in_person)
print("hey,"+full_mes+"!")
#输出
What's your car's name
(caution:enter 'q' at any time to quit )
your name: Toyota
year is: 3
person: Alway
hey,Alway have Toyota for 3 years!
What's your car's name
(caution:enter 'q' at any time to quit )
your name: q
循环:
#% 求模运算
num=input("please input a number:")
num_int=int(num) #字符串无法与整数比较,应该转换为数值表示
if num_int%2==0:
print("it is a even")
print("it is a odd")
#输出 略
prompt="\nTell me your name,and I will repeat it back to you :"
prompt+="\nEnter 'quit' to end it."
#这个示例演示了一种创建多行字符串的方式。
#第1行将消息的前半部分存储在变量prompt中;
#在第2行中,运算符+=在存储在prompt中的字符串末尾附加一个字符串。
message=""#创建一个变量用于存储用户的值
#Python首次执行while语句时,需要将message的值与'quit'进行比较,但此时用户还没有输入。
#如果没有可供比较的东西,Python将无法继续运行程序。为解决这个问题,我们必须给变量message指定一个初始值。
#虽然这个初始值只是一个空字符串,但符合要求,让Python能够执行while循环所需的比较。
while message!="quit":
message=input(prompt)
if message !='quit':
print(message)
#输出
Tell me your name,and I will repeat it back to you :
Enter 'quit' to end it. alway
alway
Tell me your name,and I will repeat it back to you :
Enter 'quit' to end it.
Tell me your name,and I will repeat it back to you :
Enter 'quit' to end it. quit
——————————————————————————————————————————————————————————————————
prompt="\nTell me your name,and I will repeat it back to you :"
prompt+="\nEnter 'quit' to end it."
active=True
while active:
message=input(prompt)
if message=='quit':
active=False
#break #也可以这样使用break退出循环
else:
print(message)
cur_num=0
while cur_num<=100:
cur_num+=1
if cur_num%3!=0:
continue
else:
print(cur_num)
#如果程序陷入无限循环,可按Ctrl+C,也可关闭显示程序输出的终端窗口
#输出:打印0~100能被3整除的整数。
|