其他分享
首页 > 其他分享> > 三_06 汇率对换1.0(1)

三_06 汇率对换1.0(1)

作者:互联网

目录

1 案例描述

2 案例分析

3 上机实验

3.1 文件运行文件报错:

Alt
分析问题:
SyntaxError: Non-ASCII character ‘\xe8’ in file E:/Python_course/lect02/currency_converter_v1.0.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

翻译:
语法错误:
在‘E:/Python_course/lect02/currency_converter_v1.0.py ’的文件的第二行有非ASCII码字符,但是没有编码声明;详细信息查看 http://python.org/dev/peps/pep-0263/
问题确定:
使用中文字符编码而未加编码提示。1

解决方案:
文件开头加入编码声明 # coding= utf-8

# coding= utf-8

3.2 无法在run窗口运行

问题描述:使用时总是在Python Console中运行。
Alt
问题定位:参考 CSDN博客:pycharm运行程序时在Python console窗口中运行
运行配置(configuration)设置差异。
问题解决:
Alt
Alt

3.3 eval函数报错

问题描述:
Alt
问题分析:
TypeError: eval() arg 1 must be a string or code object

参考:
Python中eval()和input()的用法浅析

问题定位:
input()函数得到的结果理解不当。
实际测试input()得到的结果为int型

# coding= utf-8
# -*- coding:utf-8 -*-

rmb_str_value = input('请输入人民币(CNY)金额:')

print(type(rmb_str_value))

结果:
Alt

解决方案:
去掉eval()2语句。

3.4 print输出显式格式出错

问题描述:
代码:

print('美元(USD)金额是:', usd_value)

结果:
Alt
问题分析:
输出现实问题
参考:
彻底搞懂 python 中文乱码问题
Python输出函数print()总结(python print())

问题定位:

  1. print()函数的使用格式存在问题。
  2. 文件的编码保存方式为 UTF-8;而CMD默认(终端显示)的编码方式为ASCII;显示为汉语以utf-8(一个汉字占用3个字节)方式保存的编码。

问题解决:
修改print()函数输出格式:

print('美元(USD)金额是:%f'%usd_value)

结果:
Alt


  1. 在python官网得到如下帮助信息:
    Python will default to ASCII as standard encoding if no other encoding hints are given.
    翻译:如果没有其他编码提示,Python将默认为ASCII作为标准编码。
    To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file
    翻译:要定义源代码编码,必须在源文件中第一行或第二行放置一个魔术注释) ↩︎

  2. python中使用help(eval)得到结果如下:
    eval(...) eval(source[, globals[, locals]]) -> value Evaluate the source in the context of globals and locals. The source may be a string representing a Python expression or a code object as returned by compile(). The globals must be a dictionary and locals can be any mapping, defaulting to the current globals and locals. If only globals is given, locals defaults to it.
    python中eval函数作用 ↩︎

标签:编码,06,Python,对换,问题,python,eval,print,1.0
来源: https://blog.csdn.net/kobeno1love/article/details/88968204