我今天一直在写一个程序(一个刽子手游戏),并遇到了一些障碍,我知道你们不喜欢用这个作为家庭作业答案的人,但我有搜索了很长一段时间,虽然我在开发早期发布了这个程序,但我已经开辟了新的领域。我正在处理列表,元组,词典等等,这个程序一直很棒。对不起文字墙!
问题:
使用列表时有没有办法引用字符串?
上下文 - 定义一个函数(最后一行是测试是否有效)
def click_1 (text):
key_1 = word.index [letter]
hidden_word[key_1] = letter
print (hidden_word)
这引发了一个很长的错误,我不会发布,这里是重要的'在我眼中:
File "/Users/foo/Desktop/Hangman.py", line 19, in click_1
key_1 = word.index [letter]
TypeError: 'builtin_function_or_method' object is not subscriptable
如果有人可以帮助我,那就太好了。我尝试过使用''声明,但我无法使其工作 - 目前,虽然我怀疑它可能是解决方案的一部分。我试过了:
key_1 = word.index [letter in word]
和
key_1 = word.index([letter] in word)
这两个都不起作用。
提前为一位年轻的程序员干杯!
答案 0 :(得分:2)
我不知道你的功能是支持做什么的,但这是你遇到的问题:
def click_1(text):
key_1 = word.index [letter]
# word.index(letter) calls the function,
# word.index[letter] tries to treat the function like a dictionary
# and pull up its value attached to the key `letter`
hidden_word[key_1] = letter
print (hidden_word)
或者我建议看一下不同的设计模式。请尝试改为:
the_word = generate_random_word_from(word_list)
# which is probably as simple as random.choice(word_list)
guessed_letters = set()
def display_word():
global the_word
global guessed_letters
# globals are bad in practice, but this is actually a great use case
# for a global if you haven't wrapped this all up in OOP yet!
masked_list = [letter if letter in guessed_letters else "_" for
letter in the_word]
# this is literally:
# # masked_list = []
# # for letter in the_word:
# # if letter in guessed_letters:
# # masked_list.append(letter)
# # else:
# # masked_list.append("_")
masked_word = ''.join(masked_list)
# join the list on an empty string, e.g.:
# ['s','_','m','e','w','_','r','d'] becomes 's_mew_rd'
print(masked_word)
# this wouldn't be a bad place to trigger your win condition, too.
# Could be as simple as
# # if "_" not in masked_word:
# # you_win()
def guess_letter():
global the_word
global guessed_letters
guessed_letter = prompt_for_letter_somehow()
if guessed_letter not in the_word:
add_hangman_piece() # put another arm/leg/whatever on your dude
guessed_letters.add(guessed_letter)
# add that letter to the guessed_letters set anyway
# validation would be nice here so you can't just keep guessing the same letters
答案 1 :(得分:1)
这是我很久以前为计算机科学的mitx介绍所做的一个刽子手游戏,你可能会发现它很有用,它展示了如何在不需要任何全局声明的情况下制作游戏:
from string import ascii_lowercase
def is_word_guessed(secret_word, letters_guessed):
return all(x in letters_guessed for x in secret_word)
def get_available_letters(letters_guessed):
return "".join([x for x in ascii_lowercase if x not in letters_guessed])
def get_guessed_word(secret_word, letters_guessed):
return "".join([letter if letter in letters_guessed else "_" for letter in secret_word])
def hangman(secret_word):
print "Welcome to the game Hangman!"
print "I am thinking of a word that is {} letters long\n-----------".format(len(secret_word))
guesses = 8
letters_guessed = []
missed_l = ''
correct_l = ''
while True:
if is_word_guessed(secret_word, letters_guessed):
print "Congratulations, you won!"
break
elif len(missed_l) == 8:
print "Sorry, you ran out of guesses. The word was {}.".format(secret_word)
break
print "You have {} guesses left".format(guesses)
print "Available Letters: {}".format(get_available_letters(letters_guessed))
l = raw_input("Please guess a letter: ").lower()
if l in letters_guessed:
print "Oops! You've already guessed that letter: {}\n-----------".format(
get_guessed_word(secret_word, letters_guessed))
elif l in secret_word:
letters_guessed.append(l)
get_guessed_word(secret_word, letters_guessed)
correct_l += l
print( "Good guess: {}\n-----------".format(
"".join([x if x in letters_guessed else "_" for x in secret_word])))
elif l not in secret_word:
letters_guessed.append(l)
guesses -= 1
print "Oops! That letter is not in my word: {}\n-----------".format(
get_guessed_word(secret_word, letters_guessed))
missed_l += l
hangman("foobar")