我一直在努力解決如何把一個文本文件隨機挑選一個單詞,並從3個不同的文本文件中完成1行打印,但我不能把它放在1行?它每次都把它放在一個新的路線上?這些單詞全部在文本文件的列中。使用PYTHON IDLE向上註冊即時消息!Python,從文本文件打印並放在1行?
###############
import random
import time #Importing so some bits of code works
import sys
###############
#The main game and generator
def main():
#Gets text files from the folder and randomly picks a line of the text
with open("column1.txt") as A:
A = random.choice(list(A))
with open("column2.txt") as B:
B = random.choice(list(B))
with open("column3.txt") as C:
C = random.choice(list(C))
print("\nThou\n"A+B+C)
time.sleep(2)
print("Restarting...\n")
menu()
#Menu System
def menu():
print("===Menu===")
print("Choose 1 to start the abuse generator.")
print("Choose 2 to exit.")
choice = input("Please choose one: ")
if choice == "1":
main()
elif choice == "2":
sys.exit()
else:
print("Error!")
#Go's to the menu that is defined as menu above
menu()
這是輸出:
===Menu===
Choose 1 to start the abuse generator.
Choose 2 to exit.
Please choose one: 1
Thou
lumpish
tdizzy-eyed
thugger-mugger
Restarting...
這裏是列1:
artless
bawdy
beslubbering
bootless
churlish
cockered
clouted
craven
currish
dankish
dissembling
droning
errant
fawning
fobbing
froward
frothy
gleeking
goatish
gorbellied
impertinent
infectious
jarring
loggerheaded
lumpish
mammering
mangled
mewling
paunchy
pribbling
puking
puny
quailing
rank
reeky
roguish
ruttish
saucy
spleeny
spongy
surly
tottering
unmuzzled
vain
venomed
villainous
warped
wayward
weedy
yeasty
你能提供一個格式化文件的例子嗎?我們可以看到column1.txt的佈局嗎? – Panda