此代码之前工作正常,但是现在我遇到了while 0 <= int(relationship)> 3代码块的问题。用户应该能够选择与信件接收者的关系类型,这将把称呼和称呼填充到信件中。
如果我将if / elif语句保留在while循环中,则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,并且必须再次提示,则亲爱的和亲切的用户不会填充。
答案 0 :(得分:0)
在循环中,您不会将input
的返回值转换为整数。