我在想,在python中,我怎么能让变量,比如一个单词,像一个数字一样,来创建一个点的系统,并将其写入我的计算机上的一个文本文件中
def pointsystem():
import easygui
points = open ('totalpoints.txt', 'r')
readpts = points.readline()
points.close
pointsinput = easygui.enterbox("enter paretal password")
if pointsinput == 'testpass':
pointsadd = easygui.enterbox("enter points")
pointsedit = open ('totalpoints.txt', 'a')
pointsedit.write(readpts + pointsadd)
pointsedit.close()
我认为
readpts = int(readpts)
和pointsadd = int(pointsadd)
可以实现这个技巧,将这些字符串转换成数字。然后在该写的时候用str(readpts + pointsadd)
把它们改回来。你知道吗基于注释编辑:实际上,您不能将数字作为整数类型存储在文件中(或者,如果可以的话,那就太麻烦了)。如果要用全新的数字替换文件中的数字,而不是不断地添加新的数字,请使用
pointsedit = open ('totalpoints.txt', 'w')
而不是pointsedit = open ('totalpoints.txt', 'a')
。这将删除文件的原始内容并用新内容(新点数)替换它,而不只是添加越来越多的数字。但它必须作为字符串读入,然后作为字符串写出。这真的容易多了。你知道吗相关问题 更多 >
编程相关推荐