【问题标题】:I'm getting Invalid Syntax with p_desc highlighted when I run this code当我运行此代码时,我得到了无效的语法,并突出显示了 p_desc
【发布时间】:2019-04-10 19:51:57
【问题描述】:

该代码应该将项目添加到多个字典(unit_price、description、stock)。但问题是当我运行代码时,变量名称 p_desc 会突出显示。

我尝试删除导致 p_stock 突出显示的整个语句。只有当整个 elif 语句被删除时,代码才会运行。

while(c!= "q" or c!= "Q"):

    c= input("What would you like to do?")
    if(c=="q" or c=="Q"):
        break

    elif(c=="A" or c=="a"):

        p_no = int(input("Enter part number: "))
        p_pr = float(input("Enter part price: ")
        p_desc = input("Enter part description: ")
        p_stock = int(input("Enter part stock: "))


        for i in range(0,len(unit_price)):
            if(p_no in unit_price):
                print(p_no,"That part number already exists :(, changing value to")
                p_no+=1

        unit_price.update({p_no: p_pr})
        description.update({p_no: p_desc})
        stock.update({p_no: p_stock})
        print()

【问题讨论】:

  • 你的代码中有很多语法错误,我建议删除这些并再次打印代码。为了将来的目的,请使用像 jetbrains.com/pycharm 这样的 Python IDE,它会为您突出显示语法
  • 只有一个 ) 在行:p_pr = float(input("Enter part price: ")

标签: python python-3.x syntax-error


【解决方案1】:

你还没有关闭上面一行中的括号

  p_pr = float(input("Enter part price: ")

你需要关闭两个括号

  p_pr = float(input("Enter part price: "))

【讨论】:

    【解决方案2】:

    您错过了 2 个右括号。

    变化:

    p_pr = float(input("Enter part price: ")
    p_desc = input("Enter part description: ")
    

    收件人:

    p_pr = float(input("Enter part price: "))
    p_desc = input("Enter part description: "))
    

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2018-12-09
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多