尝试从文本文件的随机行添加文本
Trying to add text from a random line from a text file
我正在 pygame 为我姐姐在 pygame 练习游戏,我正在尝试从文本文件中随机添加一行到 window 名称:
导入pygame、随机、系统
RandomMess = open('GameText.txt')
pygame.display.set_caption('Dream Land: {}').
#The .txt file currently has 4 lines but will be a lot more in the future...
#These are the test lines: This is in beta, hi :), hello, hi
我有所有剩余的代码,以防有人说我没有编写任何其他代码来制作 window 或任何东西。
我想要它说:Dream Land: {} 然后在大括号中添加来自文件的随机行。
您正在寻找 readlines() 和 random.choice()。
import random,sys
RandomMess = open('GameText.txt')
# .readlines() will create a list of the lines in the file.
# So in our case ['This is in beta', 'hi :)', 'hello', 'hi']
# random.choice() then picks one item from this list.
line = random.choice(RandomMess.readlines())
# Then use .format() to add the line into the caption
pygame.display.set_caption('Dream Land: {}'.format(line))
我正在 pygame 为我姐姐在 pygame 练习游戏,我正在尝试从文本文件中随机添加一行到 window 名称:
导入pygame、随机、系统
RandomMess = open('GameText.txt')
pygame.display.set_caption('Dream Land: {}').
#The .txt file currently has 4 lines but will be a lot more in the future...
#These are the test lines: This is in beta, hi :), hello, hi
我有所有剩余的代码,以防有人说我没有编写任何其他代码来制作 window 或任何东西。 我想要它说:Dream Land: {} 然后在大括号中添加来自文件的随机行。
您正在寻找 readlines() 和 random.choice()。
import random,sys
RandomMess = open('GameText.txt')
# .readlines() will create a list of the lines in the file.
# So in our case ['This is in beta', 'hi :)', 'hello', 'hi']
# random.choice() then picks one item from this list.
line = random.choice(RandomMess.readlines())
# Then use .format() to add the line into the caption
pygame.display.set_caption('Dream Land: {}'.format(line))