【发布时间】:2022-01-09 08:27:49
【问题描述】:
我正在尝试制作一个类似刽子手的游戏,但是当我运行我目前拥有的这段代码时,我不断收到错误,我尝试制作新变量,例如 int1 和 int2 但无济于事。代码如下:
from nltk.corpus import words
import random
word_list = words.words()
password = random.choice(word_list)
finish = len(password)
print("The password has", len(password),"letters")
print(password)
while finish > 0:
guess = input("Please enter a letter to see where it is in the word: ")
occur = password.count(guess)
mylist = {', '.join([str(i) for i, c in enumerate(password) if c == guess])}
print(mylist)
int1 = [int(y) for y in mylist]
int2 = [x+1 for x in mylist]
print(ints)
if len(guess) > 1 or len(guess) < 1:
print("Enter 1 standard keyboard letter to see if it is in the word")
elif guess in password:
print(f"This letter is at position {', '.join([str(i) for i, c in enumerate(password) if c == guess])} in the word")
finish = finish - occur
password = password.replace(guess,'.')
print("Only", finish, "letters left to guess")
else:
print("Try again")
我希望第 16-22 行首先将列表中的元素从字符串转换为整数,然后打印出列表,然后再次打印出相同的列表,但将 1 添加到列表的每个元素. 这是我不断收到的错误:
Traceback (most recent call last):
File "/Users/ARR2K18/Desktop/Hangman.py", line 29, in <module>
int1 = [int(y) for y in mylist]
File "/Users/ARR2K18/Desktop/Hangman.py", line 29, in <listcomp>
int1 = [int(y) for y in mylist]
ValueError: invalid literal for int() with base 10: '1, 7'
需要帮助
【问题讨论】:
标签: python arrays list valueerror