python 自带的ide 不能保存文件

论坛 期权论坛 脚本     
已经匿名di用户   2021-11-19 14:38   2497   0

初学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)

转载于:https://www.cnblogs.com/shuiliuhualuo/p/5803126.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP