[英]Print console output to text file
import random
import time
import sys
# variable declaration
user_cave=0
cave=0
result=""
playagain="Y"
while playagain=="Y" or playagain=="y":
fo=open("save_game",'w')
print("\nYou are in the Kingdom of Dragons.\nIn front of you, you see two caves.\nIn one cave, the dragon is friendly and will share his treasure with you.\nThe other dragon is hungry and will eat you on sight!\nGood luck on your journey\n")
while True: # including while true because program should till break
try: # repeat till user give correct value
user_cave=int(input("Please select cave 1,2,3,4 or 5 : "))
if user_cave>0 and user_cave<6:
break
else:
continue
except:
""
#create random number to differentiate
cave=random.randrange(1,3)
# Bad dragon
if cave==1:
result="Gobbles you down!"
# Good Dragon
elif cave==2:
result="Greets you before share his treasure!"
print("\nYou approach the cave....",user_cave)
time.sleep(1.5)
print("A large dragon jumps out in front of you!")
time.sleep(1.5)
print("HE OPENS HIS JAWS AND.....\n")
time.sleep(2)
print(result,"\n")
playagain=input("Do you want to play again ? [y/n] : ")
sys.stdout = open("File location",'w')
sys.stdout.close()
我想將所有打印語句輸出打印到一個文本文件中。 如果用戶選擇在不關閉程序的情況下再次玩游戲,則需要附加文件,但如果用戶關閉並再次運行程序,則需要用新輸出覆蓋文件。
我想是的,創建一個新的文件名 xx_time 每場比賽
now_time = time.strftime("_%Y_%m_%d_%H_%M_%S]", time.localtime())
# All info about the game is contained in this file
file_path = os.path.join("/your_folder", "game_file{}.txt".format(now_time))
創建一個 function 來打印和寫入信息到 txt 文件
def print_and_write(file_object, info):
print(info)
file_object.write(info)
如果你想把所有的打印語句都保存到一個文本文件中,你可以把你想保存在文件中的東西復制到一個變量中,然后把它們寫到文本文件中。 例如,我將您的代碼編輯為:
import time
import sys
# variable declaration
user_cave = 0
cave = 0
result = ""
playagain = "Y"
while playagain == "Y" or playagain == "y":
f = open("Dragon Game.txt", 'w')
f.write("")
print(
"\nYou are in the Kingdom of Dragons.\nIn front of you, you see two caves.\nIn one cave, the dragon is friendly and will share his treasure with you.\nThe other dragon is hungry and will eat you on sight!\nGood luck on your journey\n")
while True: # including while true because program should till break
try: # repeat till user give correct value
user_cave = int(input("Please select cave 1,2,3,4 or 5 : "))
if user_cave > 0 and user_cave < 6:
break
else:
continue
except:
""
# create random number to differentiate
cave = random.randrange(1, 3)
# Bad dragon
if cave == 1:
result = "Gobbles you down!"
# Good Dragon
elif cave == 2:
result = "Greets you before share his treasure!"
txt = f"\nYou are in the Kingdom of Dragons.\nIn front of you, you see two caves.\nIn one cave, the dragon is friendly and will share his treasure with you.\nThe other dragon is hungry and will eat you on sight!\nGood luck on your journey\nYou approach the cave....{str(user_cave)}\nA large dragon jumps out in front of you!\nHE OPENS HIS JAWS AND.....\n{result}"
f = open("Dragon Game.txt", "a")
f.write(txt)
print("\nYou approach the cave.... " + str(user_cave))
time.sleep(1.5)
print("A large dragon jumps out in front of you!")
time.sleep(1.5)
print("HE OPENS HIS JAWS AND.....\n")
time.sleep(2)
print(result, "\n")
playagain = input("Do you want to play again ? [y/n] : ")
只是,在運行這個文件之前,你必須創建一個名為Dragon Game.txt
的文件來運行代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.