所以我正在制作一個python游戲,python程式中的一切都很好,除了寫作部分
這是 GameFile2.py 的一部分:
class lostToss():
def botChoose(self):
print("Bot is choosing between batting and bowling.")
for dot in range(5):
print((dot 1) * ".")
time.sleep(0.25)
for x in range(10):
print("")
choices = ['Bat', 'Bowl']
botting = random.choice(choices)
print(f"And the bot has chose to {botting}")
time.sleep(1)
if botting == "Bat":
f = open("GamePoints.txt","w")
with open('GamePoints.txt, 'a ') as f:
f.write('52L05yt0smdwPMA4wgdTUF7Yh4dLT')
print('ok')
time.sleep(10)
import GameFile3
elif botting == "Bowl":
file = open("GamePoints.txt","w ")
with open('GamePoints.txt', 'a ') as file:
file.write('69L05yt0smdwPMA4wgdLOL7Yh4dLT')
time.sleep(2)
import GameFile3
問題出在第 15 行和第 22 行,我多次運行檔案并列印了“ok”文本,但代碼無法寫入檔案中。
任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
with在陳述句之前省略檔案打開。像這樣
f = open("GamePoints.txt","w") # remove this line
with open('GamePoints.txt', 'a ') as f:
file = open("GamePoints.txt","w ") # remove this line
with open('GamePoints.txt', 'a ') as file:
在第一個塊中,您缺少 .txt 之后的結束引號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/498109.html
