【问题标题】:Python project printing output incorrectly and index off by onePython项目打印输出不正确并索引减一
【发布时间】:2017-03-21 09:57:02
【问题描述】:

除两件事外,此程序中的所有内容都按预期工作。如果您输入一个男孩名字,它将正确打印所有内容,除了它会添加打印行“(名字)未在前 100 个女孩名字中排名......”,输入女孩名字时的结果相同。关于如何让它停止的任何想法?此外,返回的 'pos' 也差了一个 - 有没有我可以添加的计数器来解决这个问题?

 try:
        boyfile = open("boynames2014.txt", "r")
        girlfile = open("girlnames2014.txt", "r")
        boynames = [line.strip() for line in boyfile]   #read the content to a list
        girlnames = [line.strip() for line in girlfile] #read the content to a list

except IOError:
    print("Error: file not found")


#Input gender
#search names in lists

gender = input("Enter gender (boy/girl): ")
if gender == "boy" or gender == "girl":
    name = (input("Enter name to search for: "))
else:
    print("Invalid gender")


if name in boynames:
    pos = boynames.index(name)
    print(name, "was ranked #", pos, "in 2014 for boy names")
else:
    print(name, "was not ranked in the top 100 boy names for 2014")


if name in girlnames:
    pos = girlnames.index(name)
    print(name, "was ranked #", pos, "in 2014 for girl names")
else:
    print(name, "was not ranked in the top 100 girl names for 2014")



boyfile.close()
girlfile.close()

【问题讨论】:

  • 打印(boy) was not ranked in the top 100 girl names 不是编程行为吗?因为 Python 的索引为 0,所以它减一。
  • 我想是这样,但我不想这样。如果使用男孩名,则不应打印前 100 名女孩的行

标签: python python-3.x


【解决方案1】:

您正在检查男孩和女孩的名字,无论性别,您可以添加另一个条件来检查gender

if gender == "boy" and name in boynames:
    pos = boynames.index(name) + 1
    print("{} was ranked #{} in 2014 for boy names".format(name, pos))
elif gender == "girl" and name in girlnames:
    pos = girlnames.index(name) + 1
    print("{} was ranked #{} in 2014 for girl names".format(name, pos))
else:
    print("{} was not ranked in the top 100 {} names for 2014".format(name, gender))

至于pos 为1,这是因为列表的索引为0。这意味着第一项从索引 0 开始,第二项从索引 1 开始,依此类推。因此,如果您的排名从 #1 开始,那么您只需将其抵消 1。

pos = boynames.index(name) + 1

最后,我建议您在阅读后关闭 try/except 中的文件,因为您不会在代码中的其他任何地方使用它们,因此无需将它们打开。

【讨论】:

  • 是的。此外,您可能希望更好地处理 Invalid gender ,否则如果您只打印它会产生一些错误和意想不到的结果。
  • 好的,非常感谢!我是 Python 和编程的新手,所以这对我来说仍然是学习期。
  • 不客气,通过更好的处理我的意思是确保name 被定义,因为你有条件地定义它。因此,要么将主代码移到定义 name 的 if 语句中,要么匹配条件。将 else 更改为 elif gender in ('boy', 'girl') 或 c.z.已经证明了。
  • 不确定我是否了解除了使用打印之外我还能对 Invalid gender 做些什么?到目前为止,这是我学到的唯一方法......有什么建议吗?一个网站阅读如何以不同的方式做到这一点?非常感谢!
  • 啊,好吧,现在这比教科书中的内容更有意义!老实说,我从这里的人那里学到的东西比我的班级更多......
【解决方案2】:

无论性别如何,您都在检查 nameboynamesgirlnames

我建议添加一个条件:

if gender == "boy":
    if name in boynames:
        pos = boynames.index(name)
        print(name, "was ranked #", pos, "in 2014 for boy names")
    else:
        print(name, "was not ranked in the top 100 boy names for 2014")

elif gender == "girl"    
    if name in girlnames:
        pos = girlnames.index(name)
        print(name, "was ranked #", pos, "in 2014 for girl names")
    else:
        print(name, "was not ranked in the top 100 girl names for 2014")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多