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()
我想將所有列印陳述句輸出列印到文本檔案中。如果用戶選擇在不關閉程式的情況下再次玩游戲,則需要附加檔案,但如果用戶關閉并再次運行程式,則需要用新的輸出覆寫檔案。
uj5u.com熱心網友回復:
如果你想把所有的列印陳述句保存到一個文本檔案中,你可以復制你想要保存在檔案中的東西并將它們存盤在一個變數中,然后將它們寫入文本檔案中。例如,我將您的代碼編輯為:
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
運行代碼的強制檔案。
uj5u.com熱心網友回復:
我想是的,每場比賽創建一個新的檔案名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))
創建一個函式來列印和寫入資訊到 txt 檔案
def print_and_write(file_object, info):
print(info)
file_object.write(info)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451574.html
下一篇:遞回-對嵌套串列求和