【问题标题】:Is there anything wrong with my code我的代码有什么问题吗
【发布时间】:2013-09-01 12:05:08
【问题描述】:
def isbn10(x):
    y = [int(i) for i in x]
    a = y[0] * 10 + y[1] * 9 + y[2] * 8 + y[3] * 7 + y[4] * 6 + y[5] * 5 + y[6] * 4 + y[7] * 3 + y[8] * 2
    checkno = a % 11
    checkno = 11 - checkno
    if checkno == 10:
        checkno = "X"
    if checkno == prompt[9]:
        print("Your ISBN10 number is correct.")
    else:
        print("Your check number is wrong. It should be " + prompt[0:9] + checkno)


def isbn13(x):
    y = [int(i) for i in x]
    even = y[1] + y[3] + y[5] + y[7] + y[9] + y[11]
    odd = y[0] + y[2] + y[4] + y[6] + y[8] + y[10]
    even = even * 3
    total = even + odd
    checksum = total % 10
    checksum = 10 - checkno
    if checksum == prompt[-1]:
        print("Your ISBN13 number is correct.")
    else:
        print("Your check number is wrong. It should be " + prompt[0:-2] + checkno)
    #ok...


def main():
    prompt = input("Please type in your ISBN number.\n")
    prompt = str(prompt)

    if len(prompt) == 10:
        isbn10(prompt)
    elif len(prompt) == 13:
        isbn13(prompt)
    else:
        print("Your ISBN number is invalid")


while True:
    main()
    if input('Continue? [y/n]') == 'n':
        break

当我运行程序时...:

请输入您的 ISBN 号。 9876543210

Traceback (most recent call last):
  File "C:\Users\yc\Desktop\Computing\computing\python\Python ISBN\isbn_checker.py", line 29, in <module>
    isbn10(prompt)
  File "C:\Users\yc\Desktop\Computing\computing\python\Python ISBN\isbn_checker.py", line 11, in isbn10
    print("Your check number is wrong. It should be " + prompt[0:9] + checkno)
TypeError: cannot concatenate 'str' and 'int' objects

【问题讨论】:

  • 错误是不言自明的。您正在尝试连接字符串和整数对象。 (使用字符串格式)
  • 您在同一个 checkno 变量中混合了数字和字符串。即使在 Python 这样的动态语言中,这样做也不利于健康。
  • 这个问题似乎跑题了,因为它要求进行代码审查; codereview.stackexchange.com
  • 实际上你的代码有很多问题。您不关注PEP-8 的事实就是其中之一。而那些evenodd 列表.. 认真吗?您可以在much nicer way 中构建它们!另外,y=[int(i) for i in x] 可以写成y = map(int, x)
  • 请学习格式化您的代码,以便您和其他人可以阅读,或者至少在互联网上分享您的代码之前这样做。这里有一些东西可以帮助您入门:python.org/dev/peps/pep-0008。我现在将清理您问题中的代码。

标签: python


【解决方案1】:

checkno 在这种情况下是一个整数,而您正试图将它与一个字符串连接。

checkno 替换为str(checkno)

print("Your check number is wrong. It should be " + prompt[0:9] + str(checkno))

或者,最好使用format() 而不是串联:

print("Your check number is wrong. It should be {}{}".format(prompt[0:9], checkno))

还有:

  • checkno 变量未在 isbn13() 函数中定义
  • 程序中没有main()函数
  • 代码难以阅读和理解。原因之一是它根本不遵循PEP-8 风格

【讨论】:

  • 或者,如果他们使用的是 Python3,那么 print("Your check number is wrong. It should be", prompt[0:9], checkno) 或以其他方式使用合适的字符串格式 .format 而不是连接,并通过显式转换来节省混乱......但是是的......很多OP的代码有问题;)
  • checkno 只是一个整数有时,见checkno = "X"
  • str(){}.format() 在任何情况下都可以正常工作
【解决方案2】:
print("Your check number is wrong. It should be " + prompt[0:9] + checkno)
TypeError: cannot concatenate 'str' and 'int' objects

实际上很容易解释,您甚至阅读了错误消息吗?

首先永远不要在同一个变量中存储不同类型的信息。

避免与 Python 中的 + 连接使用内置格式。

print("Your check number is wrong. It should be %s%d" % (prompt[0:9],checkno) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-17
    • 2021-12-25
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多