我正在尝试学习python,我在尝试一个hang子手游戏。但是,当我尝试将用户的猜测与单词进行比较时,它不起作用。我错过了什么?我的hang子手游戏的问题
import sys
import codecs
import random
if __name__ == '__main__':
try:
wordlist = codecs.open("words.txt", "r")
except Exception as ex:
print (ex)
print ("\n**Could not open file!**\n")
sys.exit(0)
rand = int(random.random()*5 + 1)
i = 0
for word in wordlist:
i+=1
if i == rand:
print (word, end = '')
break
wordlist.close()
guess = input("Guess a letter: ")
print (guess) #for testing purposes
for letters in word:
if guess == letters:
print ("Yessssh")
#guessing part and user interface here
你的代码做错了什么?什么不行? – RexE 2009-05-23 21:35:50
不知道你的具体错误是什么,但这里有一些关于重构的提示: - 使用random.randint()而不是random.random() - 要获得单词列表,可以使用open(“words .txt“,”r“)。readlines() - 除了最后一个for循环,你只是说”如果猜字“ – RexE 2009-05-23 21:37:33