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

Python 3.x 当执行下面的代码时,我收到消息error str concatenate,python-3.x,compiler-errors,Python 3.x,Compiler Errors,嗨,伙计们!我开始学习编程。当执行下面的代码时,我收到消息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 o

嗨,伙计们!我开始学习编程。当执行下面的代码时,我收到消息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
随机导入
单词列表=[“abababab”,“巴伦”,“香蕉”]
所选单词=随机。选择(单词列表)
打印(所选单词)
猜=输入(“猜单词,键入一个字母:\n”).lower()
i=0
而(我
消息错误: TypeError:只能将str(而不是“int”)连接到str


使用一个循环。

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

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
随机导入
单词列表=[“abababab”,“巴伦”,“香蕉”]
所选单词=随机。选择(单词列表)
打印(所选单词)
i=0
当我
i+=1
中,
i
是一个字符串,因为所选单词中i的
。应该使用两个不同的变量名,或者只使用一个变量和一个循环。
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