![](/img/trans.png)
[英]How can I go about creating a saving system for a text based adventure game in python?
[英]How do I go about saving text in a text-box?
我将如何在不延长代码的情况下保存多行文本?
很难用言语表达,但我会举一个我所做的例子。
wri_name=input("What do you want to call your new file? (Extension included): ")
print("Type what you want your file to contain!: ")
wri_con1=input("1> ")
with open((wri_name), 'w') as f:
f.writelines(["\n" + wri_con1])
我试图通过制作一个看似无穷无尽但仍可以保存到文件中的文本框来进行改进。 以微软记事本为例。 我正在尝试复制一个类似的程序,但我真的一直在摸索我能在这里做什么。
当您在循环中写入时,您必须在append mode
打开 - open(..., "a")
- 因为write mode
会删除以前的内容。
filename = input("What do you want to call your new file? (Extension included): ")
print("Type what you want your file to contain!: ")
while True:
text = input("1> ")
if text.lower() == "quit":
break
with open(filename, 'a') as f:
f.write(text + "\n")
或者你只需要打开一次
filename = input("What do you want to call your new file? (Extension included): ")
print("Type what you want your file to contain!: ")
# --- before loop ---
f = open(filename, 'w')
# --- loop ---
while True:
text = input("1> ")
if text.lower() == "quit":
break
f.write(text + "\n")
# --- after loop ---
f.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.