python下运行报"SyntaxError: Non-ASCII character '/xe6' "错误解决方法

本文详细介绍了Python编程中如何解决中文乱码问题,以及无返回值函数、有返回值函数的使用方法与返回值输出。通过具体代码示例,深入探讨了返回值的类型与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#无返回值函数hello(相当于过程)
def hello():
print 'hello world!'
#有返回值函数foo
#有返回值函数far,并输出print.其中bar函数中返回元组(元组语法不一定需要带圆括号)
def foo():
return ['xyz',10000,-98.6]
def bar():
print 'hello world2!'
return 'abc',[42,'python'],"Guido"
res = hello()
#调用res时,返回none。无返回值
print res
#调用aTuple时返回 return内容,及返回值内容
aTuple = bar()
print aTuple
调试,查看由于第一行代码都是中文的注释,中文会出现乱码。经过查阅,找到解决方法,即在代码开始的第一行添加如下一条语句:
# This Python file uses the following encoding: utf-8
或添加语句为:
# encoding: utf-8
即可解决中文编码问题
最终的代码如下:
# This Python file uses the following encoding: utf-8
#无返回值函数hello(相当于过程)
def hello():
print 'hello world!'
#有返回值函数foo
#有返回值函数far,并输出print.其中bar函数中返回元组(元组语法不一定需要带圆括号)
def foo():
return ['xyz',10000,-98.6]
def bar():
print 'hello world2!'
return 'abc',[42,'python'],"Guido"
res = hello()
#调用res时,返回none。无返回值
print res
#调用aTuple时返回 return内容,及返回值内容
aTuple = bar()
print aTuple
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值