当执行下面的代码时,我收到消息error str concatenate



嗨,伙计们!我开始学习编程。当执行下面的代码时,我收到消息error str concatenate。

import random
word_list = ["ababababababab", "baloon", "banana"]
chosen_word = random.choice(word_list)
print(chosen_word)
guess = input("Guess the word, type one letter: n").lower()
i = 0
while (i < len(chosen_word)):
for i in chosen_word:
if i == guess:
print(guess)
i += 1

消息错误:TypeError:只能将str(而不是"int"(连接到str

j = ""
for j in chosen_word:
if j == guess:
print(guess)

使用一个循环。

尝试这种方法,将输入放入while循环中(为什么要在括号中执行while条件?(。我更改的另一件事是使用if语句代替用于循环。

import random
word_list = ["ababababababab", "baloon", "banana"]
chosen_word = random.choice(word_list)
print(chosen_word)
i = 0
while i < len(chosen_word):
guess = input("Guess the word, type one letter: n").lower()
if guess in chosen_word:
print(f'Yep {guess} is in a word')
i += 1

相关内容

最新更新