python 刽子手代码的属性错误问题
Attribute error problems with python hangman code
我的 python 刽子手代码有些问题,我似乎无法修复它。只是为了让每个人都知道我是编码新手,并且仍在学习如何做事。如果有人能帮助我,我将不胜感激。我发现当我尝试 "append" 在 "print... dashes" part.Thank you!
下的 while 循环中的代码的任何其他部分时,也会发生同样的情况
代码如下:
import random
name = input("Hello, my name is H.A.N.K.M.A.N! What is yours?: ")
print("Hello, "+name," do you wanna play Hangman? Yes? Well why didn't you say so? Lets Goooooooo!")
def extractwords(filename,number):
myfile=open(filename+".txt","r")
details=myfile.readlines()
myfile.close()
words=[]
for i in details:
for item in i.split():
letter=True
for j in item:
if j.isalpha()==False:
letter = False
item = item.lower()
if len(item)==number and letter==True and item not in words:
words.append(item)
return words
player = "y"
while player =="y":
complete=0
guessed=[]
errors=""
characters = random.randint(5,9)
tries = characters+3
wordlist=extractwords("wordlist",characters)
word=random.choice(wordlist)
print("Testing word:",word)
print("You have "+ str(tries)+" tries left")
letters=[]
for i in word:
letters.append(i)
hword=[]
for i in range(0,characters):
hword.append("-")
dashes=""
for i in hword:
dashes = dashes+i
print("The word you need to guess is ",dashes)
while complete < characters and tries > 0:
guess = input("Please input your letter: ")
if guess in word:
guessed.append(dashes)
print("Noice job! You guessed the right letter!")
tries=tries-1
print("You have "+ str(tries)+" tries left")
print(dashes)
if len(guessed) == len(word):
print("What? How did this happen? Yuu must have been cheating!Just Kidding! You did a good job on finding the word, so a GOOD JOB is in order!")
layer = input("Would you like to play again? y/n: ")
elif guess.isalpha and guess not in word:
tries=tries-1
print("I am sorry, but your guess is a number / not part of the word that I am thinking off. Please have another go!")
errors.append(guess)
print("The letters you entered so far:", errors)
print("You have "+ str(tries)+" tries left")
elif len(guess)!=1 or "abcdefghiklmnoopqestuvwxyz":
print("Please enter a valid/single letter.")
elif guess in guessed:
print("Please enter a letter that you havent entered previously. ")
else:
pass
if tries==0:
print("You lose!The word you were supposed to guess was ",word)
player = input("Would you like to play again? y/n: ")
`
好的,我检查了您的代码,发现您遇到的问题不止一个。 没关系。您是新手。无论如何,我将逐个分析并为您描述它们,以及如何修复它们。好的,让我们开始吧:
letters=[]
for i in word:
letters.append(i)
这段代码虽然工作得很好,但却非常冗余。您可以简单地用 letters = list(word)
.
替换它
hword=[]
for i in range(0,characters):
hword.append("-")
dashes=""
for i in hword:
dashes = dashes+i
这两个一起去。您可以将第一个替换为 ['-']*characters
,将第二个替换为 '-'*characters
。请记住,当您将一个列表(在本例中,字符串是字符列表)乘以一个整数时,它 returns 是一个由原始列表的多次重复组成的列表。 space 效率更高...如果您首先定义了 dashes
,您可以使用 hword = list(dashes)
进行跟进。
guessed.append(dashes)
我已经在评论中告诉过你这个了,但我又来了。 guessed
应该采用您的 guess
值,而不是您的 dashes
值。
print(dashes) #line 46
再一次......你永远不会改变 dashes
,所以你不应该打印它。
elif guess.isalpha and guess not in word:
tries=tries-1
print("I am sorry, but your guess is a number / not part of the word that I am thinking off. Please have another go!")
errors.append(guess)
print("The letters you entered so far:", errors)
print("You have "+ str(tries)+" tries left")
elif len(guess)!=1 or "abcdefghiklmnoopqestuvwxyz":
print("Please enter a valid/single letter.")
elif guess in guessed:
print("Please enter a letter that you havent entered previously. ")
else:
pass
在检查 guess
是否在 word
之前,您应该 可能 检查它是否有效。这不仅仅是 python,这是一般的代码安全性。最基本的黑客技术是发送如此大的输入,以至于它会进入内存的另一部分。这是不好的。 在毁掉自己之前先检查一下自己。我个人会这样做:
while ...
#get input
If not is_valid(your_input):
continue
#Do what you want with the input
您必须定义 is_valid()
,但这就是要点。这段代码最酷的部分是 continue
语句。 continue
循环回到开始。
if guess in word:
guessed.append(guess)
print("Noice job! You guessed the right letter!")
tries=tries-1
print("You have "+ str(tries)+" tries left")
print(dashes)
if len(guessed) == len(word):
print("What? How did this happen? Yuu must have been cheating!Just Kidding! You did a good job on finding the word, so a GOOD JOB is in order!")
layer = input("Would you like to play again? y/n: ")
好的,首先,您将 player
误命名为 layer
。褶皱错位。另一方面,您可能应该将以 tries=...
开头并以 ...dashes)
结尾的语句(第 44-46 行)移到 if 语句之外。也许在循环的最后设置它,以便在有有效输入时遇到它?
现在,我在您的代码中看到的最大错误:dashes
。您继续使用它,但它始终具有相同的价值。您需要在 if guess in word:
条件中添加一段代码,用您的新猜测更新 hword
或 dashes
。这不是一项简单的任务,如果您的单词是 bookkeeper
,那么您必须替换 e
不是一次,而是 三次 次。
elif len(guess)!=1 or "abcdefghiklmnoopqestuvwxyz":
print("Please enter a valid/single letter.")
这个...好的,所以我知道我已经检查了这部分代码,但是这里有一个特别严重的错误。 你的字母表没法说对。字母表部分不及。我假设你的意思是 not guess in 'abcdefghijklmnopqrstuvwxyz'
,但因为你遗漏了第一部分,它 总是 计算为 True
。
player = input("Would you like to play again? y/n: ")
这是最后的那个。缩进有问题。当您的游戏结束时,它永远不会被调用,因为它在主 while
循环之外。
话虽如此,您的代码还不错。这是我的修复:
import random
name = input("Hello, my name is H.A.N.K.M.A.N! What is yours?: ")
print("Hello, "+name," do you wanna play Hangman? Yes? Well why didn't you say so? Lets Goooooooo!")
def extractwords(filename,number):
myfile=open(filename+".txt","r")
details=myfile.readlines()
myfile.close()
words=[]
for i in details:
for item in i.split():
letter=True
for j in item:
if j.isalpha()==False:
letter = False
item = item.lower()
if len(item)==number and letter==True and item not in words:
words.append(item)
return words
player = "y"
while player =="y":
complete=0
guessed=""
errors=""
characters = random.randint(5,9)
tries = characters+3
wordlist=extractwords("wordlist",characters)
word=random.choice(wordlist)
print("Testing word:",word)
print("You have "+ str(tries)+" tries left")
letters = list(word)
dashes = '-'*characters
hword = list(dashes)
print("The word you need to guess is ",dashes)
while complete < characters and tries > 0:
guess = input("Please input your letter: ")
if not(guess.isalpha() or len(guess)==1):
print('Error: invalid input.')
continue
if guess in word:
if guess in guessed:
print("Already guessed that!")
continue
guessed += guess
print("Guess correct!")
#code to update dashes
else:
print('Incorrect guess.')
errors += guess
tries -= 1
print("The letters you entered so far:", errors+guessed)
print("You have "+ str(tries)+" tries left")
if tries==0:
print('lose')
else:
print('win')
player = input("Would you like to play again? y/n: ")
我的 python 刽子手代码有些问题,我似乎无法修复它。只是为了让每个人都知道我是编码新手,并且仍在学习如何做事。如果有人能帮助我,我将不胜感激。我发现当我尝试 "append" 在 "print... dashes" part.Thank you!
下的 while 循环中的代码的任何其他部分时,也会发生同样的情况代码如下:
import random
name = input("Hello, my name is H.A.N.K.M.A.N! What is yours?: ")
print("Hello, "+name," do you wanna play Hangman? Yes? Well why didn't you say so? Lets Goooooooo!")
def extractwords(filename,number):
myfile=open(filename+".txt","r")
details=myfile.readlines()
myfile.close()
words=[]
for i in details:
for item in i.split():
letter=True
for j in item:
if j.isalpha()==False:
letter = False
item = item.lower()
if len(item)==number and letter==True and item not in words:
words.append(item)
return words
player = "y"
while player =="y":
complete=0
guessed=[]
errors=""
characters = random.randint(5,9)
tries = characters+3
wordlist=extractwords("wordlist",characters)
word=random.choice(wordlist)
print("Testing word:",word)
print("You have "+ str(tries)+" tries left")
letters=[]
for i in word:
letters.append(i)
hword=[]
for i in range(0,characters):
hword.append("-")
dashes=""
for i in hword:
dashes = dashes+i
print("The word you need to guess is ",dashes)
while complete < characters and tries > 0:
guess = input("Please input your letter: ")
if guess in word:
guessed.append(dashes)
print("Noice job! You guessed the right letter!")
tries=tries-1
print("You have "+ str(tries)+" tries left")
print(dashes)
if len(guessed) == len(word):
print("What? How did this happen? Yuu must have been cheating!Just Kidding! You did a good job on finding the word, so a GOOD JOB is in order!")
layer = input("Would you like to play again? y/n: ")
elif guess.isalpha and guess not in word:
tries=tries-1
print("I am sorry, but your guess is a number / not part of the word that I am thinking off. Please have another go!")
errors.append(guess)
print("The letters you entered so far:", errors)
print("You have "+ str(tries)+" tries left")
elif len(guess)!=1 or "abcdefghiklmnoopqestuvwxyz":
print("Please enter a valid/single letter.")
elif guess in guessed:
print("Please enter a letter that you havent entered previously. ")
else:
pass
if tries==0:
print("You lose!The word you were supposed to guess was ",word)
player = input("Would you like to play again? y/n: ")
`
好的,我检查了您的代码,发现您遇到的问题不止一个。 没关系。您是新手。无论如何,我将逐个分析并为您描述它们,以及如何修复它们。好的,让我们开始吧:
letters=[]
for i in word:
letters.append(i)
这段代码虽然工作得很好,但却非常冗余。您可以简单地用 letters = list(word)
.
hword=[]
for i in range(0,characters):
hword.append("-")
dashes=""
for i in hword:
dashes = dashes+i
这两个一起去。您可以将第一个替换为 ['-']*characters
,将第二个替换为 '-'*characters
。请记住,当您将一个列表(在本例中,字符串是字符列表)乘以一个整数时,它 returns 是一个由原始列表的多次重复组成的列表。 space 效率更高...如果您首先定义了 dashes
,您可以使用 hword = list(dashes)
进行跟进。
guessed.append(dashes)
我已经在评论中告诉过你这个了,但我又来了。 guessed
应该采用您的 guess
值,而不是您的 dashes
值。
print(dashes) #line 46
再一次......你永远不会改变 dashes
,所以你不应该打印它。
elif guess.isalpha and guess not in word:
tries=tries-1
print("I am sorry, but your guess is a number / not part of the word that I am thinking off. Please have another go!")
errors.append(guess)
print("The letters you entered so far:", errors)
print("You have "+ str(tries)+" tries left")
elif len(guess)!=1 or "abcdefghiklmnoopqestuvwxyz":
print("Please enter a valid/single letter.")
elif guess in guessed:
print("Please enter a letter that you havent entered previously. ")
else:
pass
在检查 guess
是否在 word
之前,您应该 可能 检查它是否有效。这不仅仅是 python,这是一般的代码安全性。最基本的黑客技术是发送如此大的输入,以至于它会进入内存的另一部分。这是不好的。 在毁掉自己之前先检查一下自己。我个人会这样做:
while ...
#get input
If not is_valid(your_input):
continue
#Do what you want with the input
您必须定义 is_valid()
,但这就是要点。这段代码最酷的部分是 continue
语句。 continue
循环回到开始。
if guess in word:
guessed.append(guess)
print("Noice job! You guessed the right letter!")
tries=tries-1
print("You have "+ str(tries)+" tries left")
print(dashes)
if len(guessed) == len(word):
print("What? How did this happen? Yuu must have been cheating!Just Kidding! You did a good job on finding the word, so a GOOD JOB is in order!")
layer = input("Would you like to play again? y/n: ")
好的,首先,您将 player
误命名为 layer
。褶皱错位。另一方面,您可能应该将以 tries=...
开头并以 ...dashes)
结尾的语句(第 44-46 行)移到 if 语句之外。也许在循环的最后设置它,以便在有有效输入时遇到它?
现在,我在您的代码中看到的最大错误:dashes
。您继续使用它,但它始终具有相同的价值。您需要在 if guess in word:
条件中添加一段代码,用您的新猜测更新 hword
或 dashes
。这不是一项简单的任务,如果您的单词是 bookkeeper
,那么您必须替换 e
不是一次,而是 三次 次。
elif len(guess)!=1 or "abcdefghiklmnoopqestuvwxyz":
print("Please enter a valid/single letter.")
这个...好的,所以我知道我已经检查了这部分代码,但是这里有一个特别严重的错误。 你的字母表没法说对。字母表部分不及。我假设你的意思是 not guess in 'abcdefghijklmnopqrstuvwxyz'
,但因为你遗漏了第一部分,它 总是 计算为 True
。
player = input("Would you like to play again? y/n: ")
这是最后的那个。缩进有问题。当您的游戏结束时,它永远不会被调用,因为它在主 while
循环之外。
话虽如此,您的代码还不错。这是我的修复:
import random
name = input("Hello, my name is H.A.N.K.M.A.N! What is yours?: ")
print("Hello, "+name," do you wanna play Hangman? Yes? Well why didn't you say so? Lets Goooooooo!")
def extractwords(filename,number):
myfile=open(filename+".txt","r")
details=myfile.readlines()
myfile.close()
words=[]
for i in details:
for item in i.split():
letter=True
for j in item:
if j.isalpha()==False:
letter = False
item = item.lower()
if len(item)==number and letter==True and item not in words:
words.append(item)
return words
player = "y"
while player =="y":
complete=0
guessed=""
errors=""
characters = random.randint(5,9)
tries = characters+3
wordlist=extractwords("wordlist",characters)
word=random.choice(wordlist)
print("Testing word:",word)
print("You have "+ str(tries)+" tries left")
letters = list(word)
dashes = '-'*characters
hword = list(dashes)
print("The word you need to guess is ",dashes)
while complete < characters and tries > 0:
guess = input("Please input your letter: ")
if not(guess.isalpha() or len(guess)==1):
print('Error: invalid input.')
continue
if guess in word:
if guess in guessed:
print("Already guessed that!")
continue
guessed += guess
print("Guess correct!")
#code to update dashes
else:
print('Incorrect guess.')
errors += guess
tries -= 1
print("The letters you entered so far:", errors+guessed)
print("You have "+ str(tries)+" tries left")
if tries==0:
print('lose')
else:
print('win')
player = input("Would you like to play again? y/n: ")