prompt="If you tell us who you are ,we can personalize the messages you see." prompt+="nwhat is your first name? " name=input(prompt) print(f"nhello,{name}")
输入一个数,返回奇数与偶数
number=input("enter a number, and I will tell you if it`s even or odd: ") number=int(number) if number % 2==0: print(f"n{number} is even.") else: print(f"n{number} is odd.")
#8人以上无座位
number=int(input("how many people?")) if number > 8 : print("sorry,没有空座位!") else: print("hhh,有空位!!!")
使用while输入1-5的数
current_number=1 while current_number<=5: print(current_number) current_number+=1 #1 2 3 4 5
返回不能被2整除的数
current_number=0 while current_number<10: current_number+=1 if current_number%2==0: continue else: print(current_number) #1 3 5 7 9
习题7-4
prompt='n请输入你需要添加的佐料:' prompt+='n输入"quit"停止添加n' active=True addings=[] while active: food=input(prompt) addings.append(food) if food=='quit': active=False else: print(addings)
习题7-5
prompt="How old are you?" prompt+="nenter 'quit' 退出 " active=True while active: age=input(prompt) if age=='quit': break age=int(age)
#删除列表中的特定元素
pets=['dog','cat','dog','fish','cat','rabbit'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets) #['dog', 'cat', 'dog', 'fish', 'cat', 'rabbit'] #['dog', 'dog', 'fish', 'rabbit']
使用一个列表填充另一个空列表
sandwich_orders=['apple','banana','link','waterlemon','orange'] finished_sanwiches=[] while sandwich_orders: print(sandwich_orders) sandwiches=sandwich_orders.pop() finished_sanwiches.append(sandwiches) for finished_sanwiche in finished_sanwiches: print(finished_sanwiche) """ ['apple', 'banana', 'link', 'waterlemon', 'orange'] ['apple', 'banana', 'link', 'waterlemon'] ['apple', 'banana', 'link'] ['apple', 'banana'] ['apple'] orange waterlemon link banana apple """
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)