初学python 用shell写的代码结果不能保存,经查询,原因有人说是因为文件里有中文,
import random secret =random.randint(1,100) guess=0 tries=0 print "______" while guess!=secret and tries<6: guess=input("what's you guess:") if guess<secret: print "low" elif guess>secret: print "too height" tries =tries+1 if guess==secret: print "you are lucky you right" else: print "no chance,you lose" print "right secret:"+secret
改写代码全部用英文结果成功保存
的代码中有中文,可能涉及到编码问题,在代码开头加上#coding=utf-8
what's you guess:49
too height
what's you guess:24
low
what's you guess:36
low
what's you guess:42
low
what's you guess:46
low
what's you guess:48
too height
没机会了你输了
Traceback (most recent call last):
File "F:/work/py/startWithChild/1-2/GuessNumber.py", line 21, in <module>
print "right secret:"+secret
TypeError: cannot concatenate 'str' and 'int' objects
运行结果报错,是说str类型不能和int类型直接拼接,解决方法
最后一行代码修改,用bytes() 将参数修改。
print "right secret:"+bytes(secret)