Python if 语句不适用于两个条件
Python if statement not working with two conditions
我试图在 python 中编写代码但出现错误。
use_letter=[]
word = "admit"
guess = 'b'
if guess in word and not in use_letter:
use_letter.append(guess)
这里我创建了一个列表use_letter
。如果字母已经在单词中但不在 use_letter 中,我想附加 guess
。但是,我在这一行中收到错误 if guess in word and not in use_letter:
。
错误信息:语法无效
我还想制作 use_letter 列表,一个唯一的元素列表,其中不允许有重复的元素。
如何创建唯一列表?
每次创建新条件时都需要添加猜测。试试这个:
if guess in word and guess not in use_letter:
use_letter.append(guess)
use_letter = list(dict.fromkeys(use_letter)))
最后一行删除了所有重复项。
我试图在 python 中编写代码但出现错误。
use_letter=[]
word = "admit"
guess = 'b'
if guess in word and not in use_letter:
use_letter.append(guess)
这里我创建了一个列表use_letter
。如果字母已经在单词中但不在 use_letter 中,我想附加 guess
。但是,我在这一行中收到错误 if guess in word and not in use_letter:
。
错误信息:语法无效
我还想制作 use_letter 列表,一个唯一的元素列表,其中不允许有重复的元素。
如何创建唯一列表?
每次创建新条件时都需要添加猜测。试试这个:
if guess in word and guess not in use_letter:
use_letter.append(guess)
use_letter = list(dict.fromkeys(use_letter)))
最后一行删除了所有重复项。