【问题标题】:I'm getting a SyntaxError: (unicode error) 'unicodeescape' codec我收到 SyntaxError: (unicode error) 'unicodeescape' codec
【发布时间】:2012-02-14 21:45:37
【问题描述】:

您好,我正在编写一个即将到期的简单代码。我正在使用 Wing IDE 4.1.3-1,运行程序代码时不断收到此消息

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (, line 4)

这是我的程序:

#=====================================================================
# Program:      Calculating Sales
# Programmer:   Viet-Anh Tran
# Date:         2-14-2012
#               July 1, 2011 - Revised for Python 3
# Abstract:     This program processes highest sales, lowest sales,
#               and averages the total numnber of sales. Each
#               salesperson will be asked enter the amount they sold
#               and the price of each sale.
#=====================================================================

def main():
    #create variable to control loop
    keep_going = 'y'
    #process each salperson's sales
    while keep_going == 'y' or keep_going == 'Y':
        #use a function to process sales
        show_sales()
        keep_going = input("would you like to calculate another salesperson's" +
        "sales? (enter y or Y as yes to continue) ")
#define show_sales
def show_sales():
    #ask for name of salesperson
    name = input("What is the salesperson's name? ")
    #ask for first sales amount
    print('Enter', name, "'s first sale amount: ")
    first_sale = float(input())
    #when assigning a value for first sale make sure its bewtween 1-25000
    while first_sale < 1 or first_sale > 25000:
        #prompt error message if it does not meet within range
        print('ERROR: First sale amount must fall in the range of $1-$25,000.')
        #ask user again for CORRECT amount
        print('Please enter', name, "'s correct first sale amount:")
        first_sale = float(input())
    #assign all total highest and lowest sales to first sale
    total_sale = first_sale
    highest_sale = first_sale
    lowest_sale = first_sale
    #ask for the total number of sales
    print('Are there any more sales? If so, type in the total number of sales:')
    number_sales = int(input())
    #create a loop
    for sale in range(2, number_sales + 1):
        print('What is', name, "'s next sales amount?")
        sales_amount = float(input())
        #again make sure that it is within range and ask the correct amount again
        while sales_amount < 1 or first_sale > 25000:
            print('ERROR: Sale amount must fall in the range of $1-$25,000.')
        print('Please enter', name, "'s correct sale amount:")
        sales_amount = float(input())
        #accumulate the total sales
        total_sales += sales_amount
        #this assigns the lowest and highest value to the correct variable
        if sales_amount > highest_sale:
            highest_sale = sales_amount
        if sales_amount < lowest_sale:
            lowest_sale = sales_amount
    #calculate average and print highest, lowest, and average
    average_sales = total_sales / number_sales
    print(name, "'s highest sale: $", format(highest_sale, ',.2f'), sep='')
    print(name, "'s lowest sale: $", format(lowest_sale, ',.2f'), sep='')
    print(name, "'s sale on average: $", format(average_sale, ',.2f'), sep='')

【问题讨论】:

  • 这可能不是您的 SyntaxError 的原因,但请不要使用浮点数进行货币计算 - 这并不可靠。
  • 那是确切的错误信息吗?它对应于哪个源代码行?你会怎么做才能看到这个错误信息?
  • 好的,这是完整的错误:Traceback(最近一次调用最后一次):文件“”,第 4 行,在 语法错误:(unicode 错误)'unicodeescape'编解码器不能解码位置 2-4 中的字节:截断 \UXXXXXXXX 转义(,第 4 行)[评估 TranVPR5.py]
  • 这里有一些作品。您先分配给total_sale,然后使用total_sales,最后您尝试打印之前未定义的average_sale,但没有编码问题。
  • 你以一种几乎不可读的方式混合了 "" 和 '',例如print('What is', name, "'s next sales amount?").您可以使用 3x 引号 (""") 终止多行字符串。

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


【解决方案1】:

File "&lt;string&gt;" 表明你的程序正在通过exec 运行,很可能是通过一个字节字符串传递到那里的。所以这可能是 IDE 中的一个疏忽。

错误的原因可能是您姓名中的特殊字符。 Python 3 无论如何都应该处理这个问题,但 Wing IDE 似乎试图让代码与 Python 2 兼容......但失败了。

当然,这主要是推测,但您应该考虑在命令行中运行您的脚本,而不需要任何 IDE。我做到了,并且成功了。

【讨论】:

  • 感谢您的帮助!但是我将它作为命令行打开并立即关闭。您是指程序代码或文件名中的特殊字符吗?
  • @OiugghogJkhkh:我的意思是代码中的特殊字符。你是否已经安装了 Python 3?
  • 是的,它已安装。我正在使用 python 3(命令行)打开,但它会立即关闭。
【解决方案2】:

这是 Wing 4.1.3 中的一个错误,可以通过使用“帮助”菜单中的“检查更新”来修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2021-12-15
    • 1970-01-01
    • 2022-11-10
    • 2016-10-31
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多