我的代码有问题。我尝试了几次,但在粗体部分似乎还是做不到。即使我输入的所有字母都相同,如何找出问题所在?
我是编码新手。
import random
import string
def split(word):
return list(word)
alphabet = 'abcdefghijklmnopqrstuvwxyz'
list(alphabet)
words = ['hat','pop' ,'cut']
secret_word = random.choice(words)
word_length = print("the length of the word is " + str(len(secret_word)))
correct_letters = split(secret_word)
wrong_letters_storage = []
correct_letters_storage = []
def guessing():
while True:
c = 0
while c <= 3:
print("")
print("you have guessed " + str(c) + " times")
print(correct_letters_storage)
command = input("guess: ").lower()
***if correct_letters_storage == correct_letters:
print("you win!")
break***
elif command == 'quit':
print("thank you for playing my game")
break
else:
if not command in alphabet :
print("pick an alphabet")
elif command in wrong_letters_storage:
print("you have picked this word")
else :
if command in secret_word :
print("right")
correct_letters_storage.append(command)
c += 1
elif not command in secret_word :
print("wrong")
wrong_letters_storage.append(command)
c += 1
else :
print("error")
return print("you lose")
guessing()
print(correct_letters_storage)
答案 0 :(得分:1)
您在第27行和第28行有一个非常简单的错误。您在第27行和第28行只是有一个***,您不需要此。
您的错误行如下所示:
***if correct_letters_storage == correct_letters:
print("you win!")
break***
然后您会有一些不同的错误,但是它们是合乎逻辑的。
首先在函数split()中可以使用:
word.split('')
它会做同样的事情,但这不是您的主要问题。
然后,您还有另一个错误。当您尝试拆分字母时,不会将其保存到变量中。更改后的行如下所示:
alphabet=list(alphabet)
此外,如果您输了或赢了,我已经更改了您的支票。
if correct_letters_storage == correct_letters:
print("you win!")
break
elif c>3:
print('you lose')
break
固定线如下:
# importing modules
import random
# the function is splitting a word
def split(word):
# this works just fine with list(word)
# but you could also use word.split('')
return list(word)
#setting the alphabet
alphabet = 'abcdefghijklmnopqrstuvwxyz'
# here is on error with list(alphabet) you are spliting up the string word but not savng it
# with alphabet = list(alphabet) -> it works
alphabet=list(alphabet)
words = ['hat','pop' ,'cut']
secret_word = random.choice(words)
word_length = print("the length of the word is " + str(len(secret_word)))
correct_letters = split(secret_word)
wrong_letters_storage = []
correct_letters_storage = []
def guessing():
c = 0
while c < 3:
print("")
print("you have guessed " + str(c) + " times")
print(correct_letters_storage)
command = input("guess: ").lower()
if command == 'quit':
print("thank you for playing my game")
break
else:
if not command in alphabet :
print("pick an alphabet")
elif command in wrong_letters_storage:
print("you have picked this word already")
else :
if command in secret_word :
print("right")
correct_letters_storage.append(command)
c += 1
elif not command in secret_word :
print("wrong")
wrong_letters_storage.append(command)
c += 1
else :
print("error")
if correct_letters_storage == correct_letters:
print("you win!")
break
elif c>3:
print('you lose')
break
guessing()
print(correct_letters_storage)
如果您想发表评论,请使用#
# this is a comment
此外,您不需要导入sting模块。对我来说,没有它就可以工作。