【问题标题】:If statement not returning the desired result如果语句没有返回所需的结果
【发布时间】:2019-10-08 13:43:57
【问题描述】:

我是 Python 新手,我认为我的代码问题是由于我是新手并且有一些理论或一些我还不熟悉的东西造成的。

是的,之前有人问过这个问题,但与我的不同。相信我,我尝试了所有我认为需要做的事情。

一切正常,直到我在“if 5 in silos”语句中添加了所有内容。 在我输入 6 个输入函数的值后,程序以退出代码 0 结束。没有其他任何事情发生。 for 循环未启动。

当提示输入“五”变量的内容时,我希望代码接受 103 或 106。

我正在使用 PyCharm 和 Python 3.7。

import mysql.connector

try:
    db = mysql.connector.connect(
        host="",
        user="",
        passwd="",
        database=""
    )

    one = int(input("Number of requested telephone numbers: "))
    two = input("Enter the prefix (4 characters) with a leading 0: ")[:4]
    three = int(input("Enter the ccid: "))
    four = int(input("Enter the cid: "))
    six = input("Enter case number: ")
    five = int(input("Enter silo (103, 106 only): "))

    cursor = db.cursor()
    cursor.execute(f"SELECT * FROM n1 WHERE ddi LIKE '{two}%' AND silo = 1 AND ccid = 0 LIMIT {one}")
    cursor.fetchall()

    silos = (103, 106)

    if five in silos:
        if cursor.rowcount > 0:
            for row in cursor:
                seven = input(f"{row[1]} has been found on our system. Do you want to continue? Type either Y or N.")
                if seven == "Y":
                    cursor.execute(f"INSERT INTO n{five} (ddi, silo, ccid, campaign, assigned, allocated, "
                                   f"internal_notes, client_notes, agentid, carrier, alias) VALUES "
                                   f"('{row[1]}', 1, {three}, {four}, NOW(), NOW(), 'This is a test.', '', 0, "
                                   f"'{row[13]}', '') "
                                   f"ON DUPLICATE KEY UPDATE "
                                   f"silo = VALUES (silo), "
                                   f"ccid = VALUES (ccid), "
                                   f"campaign = VALUES (campaign);")
                    cursor.execute(f"UPDATE n1 SET silo = {five}, internal_notes = '{six}', allocated = NOW() WHERE "
                                   f"ddi = '{row[1]}'")
                else:
                    print("The operation has been canceled.")
            db.commit()
        else:
            print(f"No results for prefix {two}.")
    else:
        print("Enter either silo 103 or 106.")

        cursor.close()
        db.close()

except (ValueError, NameError):
    print("Please, enter an integer for all questions, except case number.")

【问题讨论】:

  • 你不想:silos = range(103,106)if five in silos。或者如果您只想要 103 和 106,将 silos 定义为列表? silos = [103,106]
  • @CeliusStingher - 感谢您的回复。据我所知,范围查找 103 和 106 之间的值(104、105 也是),我想要做的是只有 2 个值可以在输入内容时使用。还是我做错了?
  • if five in silos: 声明没有错。在语句后使用断点并在调试模式下逐步检查发生了什么。
  • @makozaki - 我正在使用调试器,但它不起作用。如果我将红色项目符号放在 "seven = input(f"{row[1]} has been fou[...]" 旁边,它只会完成并且不显示任何内容。就像我正在正常运行整个代码,而不是调试它。
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: mysql python-3.x for-loop if-statement mysql-connector


【解决方案1】:

因为它必须是:

for row in cursor.fetchall():
    // do something

在您的代码中,cursor 返回一个由 db.cursor() 定义的 Python 类,但您需要调用 fetchall() 函数来读取其中包含的行。

您实际上是在调用 cursor.fetchall() 而没有对其进行任何操作,您可以将调用分配给一个变量,然后这样做:

result = cursor.fetchall()
for row in result:
   //do something

【讨论】:

    【解决方案2】:

    我发现了问题:我必须将cursor.fetchall() 存储到一个变量中。 在我把:eight = cursor.fetchall() 放在“silo”元组之前之后,一切都运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多