为了账号安全,请及时绑定邮箱和手机立即绑定

尝试通过用户输入关键字“exit”来打破 while 循环。

尝试通过用户输入关键字“exit”来打破 while 循环。

牧羊人nacy 2023-09-26 14:04:40
你好,我正在尝试编写我的第一个程序,只是一个简单的 SQLite 3 程序来熟悉它。首先,该计划的目标是创建一个表来保存考古动物遗骸的基本数据(因为他们对遗骸进行编目的方式是古老的自动取款机)。我的代码中的问题从第 16-35 行开始,我尝试创建一个循环来获取用户输入,然后将该数据插入表/目录中。我试图让程序识别关键字“exit”以退出循环。我一开始尝试使用带有 if & else 语句的 for 循环,但没有成功。我查看了其他几个类似的问题来寻求帮助,我最近的尝试是尝试切换到 while 循环。使用提供的当前代码,输入循环继续进行并忽略关键字“exit”。while input == 'exit'或反之亦然while input != 'exit'。我还尝试导入 sys 并让关键字“exit”使用 sys.exit() ,这只会使程序无法运行(也许我将其放置在循环中太早)。我尝试为 sys.exit() 和 break 定义函数,这也给出了关键字被忽略的相同问题。(我最初是用pycharm编写的,开始使用Visual Studio,因为社区版pycharm不再包含数据库工具)(正如你所看到的,我的代码是程序性的,我仍在努力对OOP充满信心)(我将数据库放在:内存:在下面的示例中)先感谢您。如果我没有提供有关我的问题的更简洁的信息,我深表歉意,并且很乐意提供任何其他需要的信息。import sqlite3conn = sqlite3.connect(':memory:')c = conn.cursor()cursor = conn.cursor()c.execute("""CREATE TABLE IF NOT EXISTS catalog (       number integer NOT NULL PRIMARY KEY autoincrement,       type text NOT NULL,       taxon text,       species text NOT NULL,       part text NOT NULL,       age integer,       layer text,       notes text       )""")while True:if input != 'exit':    print("Please enter individual specimen data: ")    c_number = input('Catalog #: ')    c_type = input('Type of Specimen: ')    c_taxon = input('Taxon: ')    c_species = input('Species: ')    c_part = input('Body Part: ')    c_age = input('Estimated Age: ')    c_layer = input('Sedimentary Layer: ')    c_notes = input('Notes: ')    cursor.execute("""        INSERT OR IGNORE INTO catalog(number, type, taxon, species, part, age, layer, notes)        VALUES (?,?,?,?,?,?,?,?)        """, (c_number, c_type, c_taxon, c_species, c_part, c_age, c_layer, c_notes))    conn.commit()    print('Specimen data entered successfully.')else:    if input == 'exit':        breakc.execute("""CREATE VIEW catalog (ASSELECT * FROM catalog;""")conn.close()
查看完整描述

2 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

input是一个函数,返回用户输入的值。

if input != 'exit':永远为真,因为input是一个函数,并且永远不会等于 'exit'

您需要检查输入的返回值以查看它是否与字符串“exit”匹配。


编辑:尝试以下操作- 如果您有更多提示或没有提示,则此选项应该是“可扩展的”。但有很多方法可以实现您想要做的事情。以下只是其中之一。我添加了评论,因为您似乎是 python 新手!

import sqlite3


conn = sqlite3.connect(':memory:')

c = conn.cursor()

cursor = conn.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS catalog (

       number integer NOT NULL PRIMARY KEY autoincrement,

       type text NOT NULL,

       taxon text,

       species text NOT NULL,

       part text NOT NULL,

       age integer,

       layer text,

       notes text

       )""")


while True:

      print('Please enter individual specimen data: ')

      input_prompts = [

        'Catalog #: ',

        'Type of Specimen: ',

        'Taxon: ',

        'Species: ',

        'Body Part: ',

        'Estimated Age: ',

        'Sedimentary Layer: ',

        'Notes: '

      ]


      responses = []

      response = ''

      for prompt in input_prompts: # loop over our prompts

        response = input(prompt)


        if response == 'exit':

          break # break out of for loop

        responses.append(response)

      

      if response == 'exit':

        break # break out of while loop


      # we do list destructuring below to get the different responses from the list

      c_number, c_type, c_taxon, c_species, c_part, c_age, c_layer, c_notes = responses


      cursor.execute("""

          INSERT OR IGNORE INTO catalog(number, type, taxon, species, part, age, layer, notes)

          VALUES (?,?,?,?,?,?,?,?)

          """,

                     (

          c_number,

          c_type,

          c_taxon,

          c_species,

          c_part,

          c_age,

          c_layer,

          c_notes,

          ))

      conn.commit()

      responses.clear() # clear our responses, before we start our new while loop iteration

      print('Specimen data entered successfully.')


c.execute("""CREATE VIEW catalog

AS

SELECT * FROM catalog;

""")

conn.close()


查看完整回答
反对 回复 2023-09-26
?
HUX布斯

TA贡献1876条经验 获得超6个赞

如果没有详细介绍您的代码,我相信它会因识别错误而失败?


python 要求您在 while 循环内缩进代码,因此它应该如下所示。


while True:

    if input != 'exit':

        print("Please enter individual specimen data: ")

        ...

第二个问题是您从未创建过该变量input。当你测试时if input == 'exit':它会失败。您需要决定用户在哪一点可以选择键入exit,将其保存到变量中并测试它,大致如下(我调用变量userinput是为了不覆盖input函数):


while True:

    userinput = input( 'type exit to exit' )

    if userinput != 'exit':

        print("Please enter individual specimen data: ")

        ...


查看完整回答
反对 回复 2023-09-26
  • 2 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信