注明:Sublime Text软件上不能运行提示用户输入的程序,你可使用Sublime Text来编写提示用户输入# 注意:Sublime Text软件不能运行提示用户输入的程序。
# 你可使用Sublime Text来编写提示用户输入的程序,但必须从终端运行它们。
# 如python终端 VS code..
parrot.py
# 7.1 函数input()的工作原理
message = input("Tell me something, and I will repeat it back to you:")
print(message)
greeter.py
# 编写清晰的程序
name = input("Please enter your name: ")
print(f"\nHello, {name}!")
greeter1.py
# 提示超过一行
prompt = "If you tell us who your are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print(f"\nHello, {name}!")
rollercoaster.py
# 使用int()来获取数值输入
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 48:
print("\nYou're tall enough to ride!")
else:
print("\nYou'll be able to ride when you're a little older.")
even_or_odd.py
number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)
if number % 2 ==0:
print(f"\nThe number {number} is even.")
else:
print(f"\nThe number {number} is odd.")
car.py
# 练习 7-1 汽车租赁
# 编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,
# 如“Let me see if I can find you a Subaru.”。
car = input("What kind of car would you like? ")
print(f"Let me see if I can find you a {car.title()}.")
party_size.py
# 练习 7-2 餐馆订位
# 编写一个程序,询问用户有多少人用餐。如果超过 8 位,就打印一条消息,
# 指出没有空桌;否则就指出有空桌。
party_size = input("How many people are in your dinner party tonight? ")
party_size = int(party_size)
if party_size >8:
print("I'm sorry, you'll have to wait for a table.")
else:
print("Your table is ready.")
number.py
# 练习 7-3 10 的整数倍
# 让用户输入一个数,并指出这个数是否是 10 的整数倍。
number = input("Give me a number, please: ")
number = int(number)
if number % 10 == 0:
print(f"{number} is a multiple of 10.")
else:
print(f"{number} is not a multiple of 10.")
counting.py
# 使用while循环 从1数到5:
current_number = 1
while current_number <=5:
print(current_number)
current_number +=1
parrot1.py
# 让用户选择何时退出
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
message = ""
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
parrot2.py
# 使用标志
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
cities.py
# 使用break退出循环
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.)"
while True:
city = input(prompt)
if city =='quit':
break
else:
print(f"I'd love to go to {city.title()}!")
counting1.py
# 在循环中使用continue
curren_number = 0
while curren_number < 10:
curren_number +=1
if curren_number %2 == 0:
continue
print(curren_number)
counting2.py
# 避免无限循环
x = 1
while x <= 5:
print(x)
x += 1
pizza_ingredients.py
# 练习 7-4 比萨配料
# 编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时结束循环。
# 每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
prompt = "\nWhat topping would you like on your pizza?"
prompt += "\nEnter 'quit' when you are finished: "
while True:
topping = input(prompt)
if topping != 'quit':
print(f" I'll add {topping} to your pizza.")
else:
break
film_ticket.py
# 练习 7-5 电影票
# 有家电影院根据观众的年龄收取不同的票价:
# 不到 3 岁的观众免费;3~12 岁的观众为10 美元;超过 12 岁的观众为 15 美元。
# 请编写一个循环,在其中询问用户的年龄,并指出其票价。
prompt = "How old are you?"
prompt += "\nEnter 'quit when you are finished. "
while True:
age = input(prompt)
if age == 'quit':
break
age = int(age)
if age < 3:
print(" You get in free!")
elif age <13:
print(" Your ticket is $10.")
else:
print(" Your ticket is $15.")
pizza_ingredients1.py
# 练习 7-6: 三种出路 以不同的方式完成练习7-4或练习7-5,在程序中采取如下做法:
# 在while 循环中使用条件测试来结束循环.(NOW!)
# 使用变量 active 来控制循环结束的时机.
# 使用break 语句在用户输入'quit'时退出循环.
# (练习7-4)
# 编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时结束循环。
# 每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
#
prompt = "\nWhat topping would you like on your pizza?"
prompt += "\nEnter 'quit' when you are finished: "
topping = ""
while topping != 'quit':
topping = input(prompt)
if topping != 'quit':
print(f" I'll add {topping} to your pizza.")
pizza_gredients2.py
# 练习 7-6: 三种出路 以不同的方式完成练习7-4或练习7-5,在程序中采取如下做法:
# 在while 循环中使用条件测试来结束循环.
# 使用变量 active 来控制循环结束的时机.(NOW!)
# 使用break 语句在用户输入'quit'时退出循环.
# (练习7-4)
# 编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时结束循环。
# 每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
#
prompt = "\nWhat topping would you like on your pizza?"
prompt += "\nEnter 'quit' when you are finished: "
active = True
while active:
topping = input(prompt)
if topping == 'quit':
active = False
else:
print(f" I'll add {topping} to your pizza.")
infinite_cycle.py
# 练习 7-7: 无限循环
# 编写一个没完没了的循环,并运行它
# (要结束该循环,可按Ctrl+C,也可关闭显示输出的窗口).
num = 0
while num <=1024:
print(num)
#以下附加也为死循环 可调用:num +=1
# num =1
confirmed_users.py
# 7.3 使用while循环处理列表和字典
# 7.3.1 在列表之间移动元素
# 首先创建一个待验证用户列表
# 和一个用于储存已验证用户的空列表
unconfirmed_users = ['alice', 'brian', 'candance']
confirmed_users = []
# 验证每个用户, 直到没有未验证用户为止.
# 将每个经过验证的用户都移到已验证用户列表中.
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print(f"Verifying user: {current_user.title()}")
confirmed_users.append(current_user)
# 显示所有已验证用户.
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
pets.py
# 7.3.2 删除为特定值的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
mountain_poll.py
# 7.3.3 使用用户输入来填充字典
responses = {}
# 设置一个标志,指出调查是否继续.
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答.
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
# 将回答存储在字典中.
responses[name] = response
# 看看是否还有人要参与调查.
repeat = input("Would you like to let another person respond? (Yes/ no) ")
if repeat == 'no':
polling_active = False
# 调查结束,显示结果.
print("\n--- Poll Result ---")
for name,response in responses.items():
print(f"{name} would like to climb {response}.")
sandwich_orders.py
# 练习 7-8 熟食店
# 创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;
# 再创建一个名为 finished_sandwiches 的空列表。
# 遍历列表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,
# 如 I made your tuna sandwich ,并将其移到列表 finished_sandwiches 。
# 所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ['a','b','c']
finished_sandwiches = []
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print(f"I'm working on your {current_sandwich} sandwich.")
finished_sandwiches.append(current_sandwich)
print("\n")
for sandwich in finished_sandwiches:
print(f"I made a {sandwich} sandwich.")
sandwich_orders1.py
# 练习 7-9 五香烟熏牛肉卖完了
# 使用为完成练习 7-8 而创建的列表 sandwich_orders ,
# 并确保 'pastrami' 在其中至少出现了三次。
# 在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉(pastrami)卖完了;
# 再使用一个 while 循环将列表 sandwich_orders 中的 'pastrami'都删除。
# 确认最终的列表 finished_sandiches 中未包含 'pastrami' 。
sandwich_orders = [
'pastrami', 'a', 'b', 'pastrami', 'c', 'pastrami',
'd', 'e', 'pastrami'
]
finished_sandwiches = []
print("I'm sorry,we're all out of pastrami today.")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print("\n")
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print(f"I'm working on your {current_sandwich} sandwich.")
finished_sandwiches.append(current_sandwich)
print("\n")
for sandwich in finished_sandwiches:
print(f"I made a {sandwich} sandwich.")
user_place.py
# 练习 7-10 梦想的度假胜地
# 编写一个程序,调查用户梦想的度假胜地。
# 使用类似于“If you could visit one place in the world, where would you go?”的提示,
# 并编写一个打印调查结果的代码块。
name_prompt = "\nWhat's your name?"
place_prompt = "If you could visit one place in the world, Where would you go? "
continue_prompt = "\nWould you like to let someone else respond? (yes/no) "
# 创建用户与其地点的存储字典
responses = {}
while True:
# 询问用户去哪里
name = input(name_prompt)
place = input(place_prompt)
# 存储调查结果
responses[name] = place
# 询问是否还有其他人要参与调查
repeat = input(continue_prompt)
if repeat == 'no':
break
# 打印调查结果
print("\n--- Result ---")
for name, place in responses.items():
print(f"{name.title()} would like to visit {place.title()}.")
本文介绍了如何在SublimeText中编写涉及用户输入的程序,并从终端运行它们的方法。此外,还提供了多个示例,包括如何使用Python处理用户输入、进行数值判断、循环操作等。
1803

被折叠的 条评论
为什么被折叠?



