这段代码以前可以工作,但现在我遇到了while0<;=int(relationship)>;3代码块的问题。用户应该能够选择与信件收件人的关系类型,这将在信件中填充问候语和告别语。你知道吗
如果在while循环中保留If/elif语句,sal/val字符串就不会填充到字母中。你知道吗
如果使If/elif语句与while循环齐平,则仅当用户第一次键入1、2或3时,才会填充字母的字符串。如果用户键入1、2或3之外的数字,程序将继续,但sal/val字符串将不会填充。你知道吗
最初,我只有一组if/elif语句,但我创建了两组语句(这是多余的,也没有解决问题),以查看这是否能解决问题。我也把压痕弄得一团糟。我也尝试过为每种类型的关系创建函数,但我不断收到称呼的名称错误。你知道吗
如果用户第一次按指示操作,我可以让程序工作;如果用户在第一次输入错误后被提示输入1、2或3,我可以让程序运行。但我不能让sal/val在两种情况下都填充。我觉得这个问题一定和缩进有关,但是当我改变缩进级别时,问题就没有解决。你知道吗
# Function for an infinite nested dictionary to add content to a personalized letter
def lkng_glss_lttr_bot():
lgl_num = 0
while command == 'compose':
store_lkng_glss_lttr = {}
new_lgl = {}
new_lgl_num = len(store_lkng_glss_lttr) + 1
store_lkng_glss_lttr[new_lgl_num] = new_lgl
address_acquaintence = ['Dear', 'Cordially,']
address_friend = ['Dearest', 'With warmest regards,']
address_beloved = ['My Darling', 'With all my love and affection,']
salutation = ''
valediction = ''
recipient = input("\nWhat is the recipient’s name?\n")
new_lgl['recipient'] = recipient
email = input("\nWhat is the recipient's email?\n")
new_lgl['email'] = email
print("\nWhat is your relationship to %s?" % recipient)
relationship = int(input("""Type 1 for Acquaintence
Type 2 for Friend
Type 3 for Beloved\n"""))
while 0 <= int(relationship) > 3:
relationship = input("Please type 1, 2, or 3.\n")
if relationship == 1:
for salutation in address_acquaintence:
salutation = address_acquaintence[0]
for valediction in address_acquaintence:
valediction = address_acquaintence[1]
elif relationship == 2:
for salutation in address_friend:
salutation = address_friend[0]
for valediction in address_friend:
valediction = address_friend[1]
elif relationship == 3:
for salutation in address_beloved:
salutation = address_beloved[0]
for valediction in address_beloved:
valediction = address_beloved[1]
if relationship == 1:
for salutation in address_acquaintence:
salutation = address_acquaintence[0]
for valediction in address_acquaintence:
valediction = address_acquaintence[1]
elif relationship == 2:
for salutation in address_friend:
salutation = address_friend[0]
for valediction in address_friend:
valediction = address_friend[1]
elif relationship == 3:
for salutation in address_beloved:
salutation = address_beloved[0]
for valediction in address_beloved:
valediction = address_beloved[1]
print("\nPlease inquire about %s's well being." % recipient)
inquiry = input("You can ask about the weather, or what %s has been doing to pass the time in the interim between your last exchange:\n" % recipient)
body = input("\nPlease write a few sentences to inform %s of the recent happenings in your life:\n" % recipient)
final_sentiment = input("\nPlease close the letter by expressing the importance of your friendship and your desire to spend time with %s in the future:\n" % recipient )
sender = input("\nWhat is your name?\n")
postscript = input("\nPlease write a short postscript to %s:\n" % recipient)
# Concatenate greeting and recipient
part_1 = salutation + " " + recipient + "," + "\n"
# Concatenate inquiry and body
part_2 = inquiry + " " + body + "\n"
# Concatenate part_1, part_2, final_sentiment, closing, sender, & postscript in reverse order for a non-tradional letter format
non_trdtnl_lttr = "P.S. " + postscript + "\n" + sender + "\n" + valediction + "\n" + final_sentiment + "\n" + part_2 + part_1
# Using the non-traditional letter format, reverse the order of the entire letter using an extended slice to create a Looking Glass Letter & store in dictionary
lkng_glss_lttr = non_trdtnl_lttr[::-1]
new_lgl['lkng_glss_lttr'] = lkng_glss_lttr
# Notify sender that their letter contents have been added to the Bot
print(store_lkng_glss_lttr)
print("\nYour letter to %s has been composed by the Looking Glass Letter Bot:\n" % recipient + lkng_glss_lttr)
print("\nYour Looking Glass Letter to %s will be emailed immediately." % recipient)
# Ask user to add another client
print("\nWould you like to compose another letter? Y/N")
sender_response = input().lower().strip()
if sender_response == 'y' or sender_response == 'yes':
continue
else:
print("\nWe hope you enjoyed using the Looking Glass Letter Bot.\nWe look forward to serving your Looking Glass Letter composition needs in the future.")
break
输出应该类似于:
,boB raeD
!doog m'I ?uoy era woH
!!uoy ssim I
,yllaidroC
G
.em llaC .S.P
但是,如果用户键入除1、2或3之外的内容,并且必须再次提示,那么亲爱的、真诚的用户将不会填充。你知道吗
在循环中,不能将
input
的返回值转换为整数。你知道吗相关问题 更多 >
编程相关推荐