[英]Creating a vowel variable for multiple inputs in a loop
I'm trying to create a variable within a loop that will print "an" in a response if the first letter of that response is a vowel. 我正在尝试在循环中创建一个变量,如果该响应的第一个字母是元音,它将在响应中显示“ an”。 Here's my script:
这是我的脚本:
responses = {}
polling_active = True
while polling_active:
name = input("\nWhat is your name? ")
response = input("Which kind of sandwich do you want? ")
if response[0] in ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'):
a_an = "an"
else:
a_an = "a"
responses[name] = response
repeat = input("Would you like to let another person order? (yes/no) ")
if repeat == 'no':
polling_active = False
print("\n-- Order --")
for name, response in responses.items():
print(name + " would like " + a_an + " " + response + " sandwich.")
Here's the output. 这是输出。 When just one order is taken, the variable works, but when multiple orders are taken, the
a_an
variable remains the same. 仅采用一个顺序时,该变量有效,但是同时采用多个顺序时,
a_an
变量保持不变。
Python ❯ python3 sandwich_orders.py ⏎
What is your name? Alex
Which kind of sandwich do you want? egg
Would you like to let another person order? (yes/no) no
-- Order --
Alex would like an egg sandwich.
Python ❯ python3 sandwich_orders.py
What is your name? Alex
Which kind of sandwich do you want? ham
Would you like to let another person order? (yes/no) no
-- Order --
Alex would like a ham sandwich.
Python ❯ python3 sandwich_orders.py
What is your name? Alex
Which kind of sandwich do you want? egg
Would you like to let another person order? (yes/no) yes
What is your name? Steve
Which kind of sandwich do you want? ham
Would you like to let another person order? (yes/no) no
-- Order --
Alex would like a egg sandwich.
Steve would like a ham sandwich.
The issue is that you are not saving the separator (a, an)
for each iteration. 问题是您没有为每次迭代保存分隔符
(a, an)
。 My solution saves the value within the pre-existing dictionary for use later! 我的解决方案将值保存在现有字典中,供以后使用!
if response[0].lower() in ('a', 'e', 'i', 'o', 'u'):#we can also convert to lowercase and check a modified list. Just to shorten, not necessary
a_an = "an"
else:
a_an = "a"
responses[name] = [a_an, response] #modify dict to contain [delimiter, response]
#... other code here
for name, response in responses.items():
print(name + " would like " + response[0] + " " + response[1] + " sandwich.")
#where response[0] is our delimiter and response[1] is our type
Welcome to programming! 欢迎编程! You are almost there.
你快到了 Move the
移动
if response[0] in ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'): a_an = "an" else: a_an = "a"
Inside the for loop before the print. 在打印之前的for循环内。
The problem is that you overwrite your a_an
variable each time you make an additional order. 问题是您每次下订单时都会覆盖
a_an
变量。 You need to save it instead for each order in a batch. 您需要为批次中的每个订单保存它。 One way to do it is to simply concatenate it within the while loop with the response,
一种实现方法是简单地在while循环中将其与响应连接起来,
responses[name] = a_an + ' ' + response
With this fix the rest of your program remains the same except: 使用此修复程序后,程序的其余部分保持不变,除了:
for name, response in responses.items():
print(name + " would like " + response + " sandwich.")
You are going to need another list to store the different values of a_an
for the different responses. 您将需要另一个列表来存储不同响应的
a_an
的不同值。 If not, it just takes the value of the last response. 如果不是,则仅获取最后一个响应的值。
I created a little procedure to fix your problem. 我创建了一个小程序来解决您的问题。
name = 'Alex'
#storing the sandwiches types
#new values: types.append('value')
types = ['ham','egg']
for i in range(0,len(types)):
response = types[i]
if response[0] in ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'):
a_an = "an"
else:
a_an = "a"
print(name + " would like " + a_an + " " + response + " sandwich.")
It's important to keep your evaluation list inside the for loop. 将评估列表保留在for循环中很重要。 Because of the len(types) it will evaluate the expression dinamically for all the elements in the list.
由于len(types),它将对列表中的所有元素进行动态评估表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.