Cs50 Pset4: Adieu – check50给出错误,但我的结果是正确的

huangapple go评论68阅读模式
英文:

Cs50 Pset4: Adieu - check50 gives error, but my results are correct

问题

我的任务是询问用户的姓名,直到他们按下Ctrl-D,然后以以下格式输出:Adieu, adieu, to Liesl, Friedrich, and Louisa,其中Liesl、Friedrich和Louisa是用户输入的姓名。在我的终端中,无论我输入多少个姓名,结果都是正确的,但是check50显示了一个不满意的标志。以下是我的代码:

names = []

while True:
    try:
        # 询问输入
        name = input("Name: ")
        # 将姓名添加到列表中
        names.append(name)
    except (EOFError, ValueError):
        print()
        break

print("Adieu, adieu, to", end=" ")
# 从第一个到倒数第二个,逗号分隔输出姓名
x = int(len(names)-2)
for amount in range(x):
    print(names[amount] , ",", end=" ", sep="")

# 使用 'and' 输出最后一个姓名
names.reverse()
# 如果列表中有两个姓名
try:
    print(str(names[1]), end=" ")
    print("and", str(names[0]))
# 如果列表中只有一个姓名
except (ValueError, EOFError, IndexError):
    print(str(names[0]))

希望这可以帮助你解决问题。

英文:

So my task is to ask users for names until they press control-d and then output in the format Adieu, adieu, to Liesl, Friedrich, and Louisa where Liesl, Friedrich and Louisa are names input by the user. In my terminal, the results are correct no matter how many names I input, they output correctly, but check50 show a frown. Here's my code:

names = []

while True:
    try:
        #ask for input
        name = input("Name: ")
        #put in the list
        names.append(name)
    except (EOFError, ValueError):
        print()
        break

print("Adieu, adieu, to", end=" ")
#from first to second last, output names by comma
x = int(len(names)-2)
for amount in range(x):
    print(names[amount] , "," , end=" ", sep="")

#use 'and' to output last name
names.reverse()
#if two names in list
try:
      print(str(names[1]), end=" ")
      print("and", str(names[0]))
#if one name in list
except (ValueError, EOFError, IndexError):
    print(str(names[0]))

答案1

得分: 0

使用check50链接阅读详细的错误消息,和/或重新阅读说明。当有3个或更多名字时,您的解决方案与预期的输出不匹配。

当有3个或更多名字时的预期输出:

再见,再见,Liesl,Friedrich和Louisa
再见,再见,Liesl,Friedrich,Louisa和Kurt

您的程序输出(比较逗号):

再见,再见,Liesl,Friedrich和Louisa
再见,再见,Liesl,Friedrich,Louisa和Kurt

此外,您是否阅读了有关“inflect”模块的提示?虽然不是必需的,但在另一个pset中将非常有帮助。

英文:

Use the check50 link to read the detailed error message, and/or re-read the instructions. Your solution doesn't match the expected output when there are 3 or more names.

Expected ouptut when there are 3 or more names:

Adieu, adieu, to Liesl, Friedrich, and Louisa
Adieu, adieu, to Liesl, Friedrich, Louisa, and Kurt

Your program outputs (compare commas):

Adieu, adieu, to Liesl, Friedrich and Louisa
Adieu, adieu, to Liesl, Friedrich, Louisa and Kurt

Also, did you read the Hint about the inflect module? While not required, it will be very helpful in another pset.

答案2

得分: 0

你可以在需要在问题中插入某些内容的情况下使用 join 方法,就像这样:

例如:

result = " and ".join(names)

根据情况可以类似这样。

英文:

I think you can use join method in question like this where you have to insert something in between..

Like for example

        result = " and ".join(names)

something like this depending on cases

huangapple
  • 本文由 发表于 2023年6月16日 12:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定