【发布时间】:2022-12-17 23:07:56
【问题描述】:
我正在尝试编写一个程序来查看给定字符串中的字母是否重复,但我遇到了一个问题(错误)“ValueError:list.remove(x):x 不在列表中”检查下面的代码我在我的程序中使用。
import random
word = input("write the word that you want to permutate in all different ways : ")
n = len(word)
def check() :
s = set ()
list = [0]
i = 0
while True :
i = i+1
if i == n :
break
list.append(i)
print(list)
while True :
number = random.randint(0,n-1)
list.remove(number)
checknum = random.choice(list)
if checknum == number :
checknum = random.randint(0,n-1)
if word[number] == word[checknum] :
print("there is a repetition of characters in the given string.....")
if len(list) == 0 :
break
check()
【问题讨论】:
-
您好,欢迎来到 StackOverflow!附带说明一下,您应该避免使用像
list这样的内置函数作为变量名。 -
如果您尝试删除例如会发生什么
7两次?