1
我刚刚开始为hangman游戏编写python代码,并将文字存储在文件中。我也给出了一个选项,可以根据用户的意愿添加单词。我已经编写了相同的代码,但由于某种原因,在程序重新启动之前该文件不会更新。请告诉我,哪里出错也是我碰巧遇到的开始编程的蟒蛇很长一段时间后,请记住,它可能是一个失误而导致因生锈或内存faults.Here的我的代码(仅适用于有关文件输入输出问题):python写入python文件不会立即发生(hangman游戏)
import os
def start():
wordlist = open("wordlist_hangman",'a+')
words= wordlist.read()
choice=menu()
if choice=='1':
os.system('cls' if os.name == 'nt' else 'clear')
game_start(wordlist,words)
elif choice=='2':
os.system('cls' if os.name == 'nt' else 'clear')
add_word(wordlist)
elif choice=='3':
os.system('cls' if os.name == 'nt' else 'clear')
print words
start()
else:
os.system('cls' if os.name == 'nt' else 'clear')
print('Invlaid input:must enter only 1,2 or 3 ')
start()
def menu():
print('Enter the number for the desired action.At any point in time use menu to go back to menu.')
print('1.Start a new game.')
print('2.Add words in dictionary.')
print('3.See words present in dictionary')
menu_choice=raw_input('>')
return menu_choice
def add_word(wordlist):
print("Enter the word you wish to add in ypur game")
wordlist.write("\n"+raw_input('>'))
start()
start()
您必须使用'wordlist.close()'来刷新文件。 –
会closinbg并重新打开文件做这项工作? – 7h3wh173r48817
但是最好使用'with'代替。删除缩进并关闭文件 –