因此,我在下面看到的循环中接受用户输入。
mail_list = []
x = 0
while x >= 0:
user_input = input('enter first name, last name, ID, and email domain: ')
mail_list.append(user_input)
if user_input == 'done':
break因此,它应该添加的名字,姓氏,电子邮件域名在一起,以形成一个完整的电子邮件地址。不确定清单是最好的去路,还是把春天剪接在一起。当我试图拼接时遇到的问题,它只会使用‘to’作为最后一个输入,所以我假设将它添加到列表中将是最好的路径。只是迷失了
发布于 2022-11-05 05:36:21
,你在期待像
email = first_name + '.' + last_name + '@gmail.' + domain这样的东西吗?
我认为高塔姆·切蒂亚尔的评论是对的。
此时,您有包含电子邮件地址的所有部分的mail_list,您可以手动加入。
email = mail_list[0] + '.' + mail_list[1] + '@gmail.' + mail_list[2]这不是一个优雅的解决方案,但是如果您真的不想创建更多的变量,或者(更好的)同时输入所有地址,我认为这是唯一的方法。
发布于 2022-11-05 05:38:14
IIUC,你可能想要这样的东西:
mail_list = []
while True: # use an infinite loop
user_input = input('enter first name, last name, ID, and email domain: ')
# check if we're done before doing anything else
if user_input == 'done':
break
# try to split the string into 4 chunks
try:
first, last, ID, domain = map(str.strip, user_input.split(','))
except ValueError:
print('Invalid format, please try again')
continue
# do something with the input
# here creating a first.last@domain string
mail_list.append(f'{first}.{last}@{domain}')
print(mail_list)示例:
enter first name, last name, ID, and email domain: first, last, ABC, example.org
enter first name, last name, ID, and email domain: first2, last2, DEF
Invalid format, please try again
enter first name, last name, ID, and email domain: first2, last2, DEF, example.org
enter first name, last name, ID, and email domain: done
['first.last@example.org', 'first2.last2@example.org']https://stackoverflow.com/questions/74325280
复制相似问题