python 自帶的ide 不能保存文件


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM