您好,我正在用 python 制作游戏,并且我让游戏在文本文档上写入数据,有没有一种方法可以编码,所以如果文本文件显示名为 Bob 的人处于 4 级,则让程序启动在第 4 级。我尝试使用 for 循环来完成这项工作,但它不会工作。它不会启动文本文件,只是转到级别 1。这是游戏代码(用于读取和写入:
import os
#---------------------------
os.system("color a")
#---------------------------
def ping(num):
os.system("ping localhost -i", num, ">nul")
def cls():
os.system("cls")
#--------------------------
print("the game")
ping(2)
cls()
print("1: New Game")
print("2: Continue")
print("3: Credits")
while True:
choice=input("Enter")
if choice==1:
name=input("Enter your name")
firstsave=open("data.txt", "W")
firstsave.write(name, " ")
# there will be the game data here
elif choice==2:
opendata=file("data")
#opening the file
while True:
''' is the place where the file scanning part needs to come.
after that, using if and elif to decide which level to start from.(there are a total of 15 levels in the game)
'''
文本文件:
User Level
Bob 5
George 12