在字符串中查找子串与列表进行比较。

huangapple go评论67阅读模式
英文:

Find substring in string compared to list

问题

I am using python to create a quiz for a school project. I want to have a list of names and question answers that people cannot input. (BC they are inappropriate). Whenever i use the code i have now, it says that any input is one of the blocked ones. I am using an in-browser editor(No pip installations are available to me).

This is my code. I have the list of blocked strings in a variable called "Banned". At first this piece of code worked, but eventually when anyone played it, the code would block all inputs, not just the ones in the Banned list.

import os
import random
print("No Capitals!")

cheatcode = ""
randlist = ["Shiny Cupcake","Good Puppy","Sunshine","Pretty Painting","Happy Trees","Soft Kitten","in love","starstuck"]
randname = random.randrange(0,6)
playscore = 0
question = 0
idkbanned = ["idk","na","n/a","urmom","ur mom","yourmom","your mom","duh"]
banned = ["ur","mom","your mom","yourmom","daddy"]
notallowed = ("Warning, Idk, N/A and duh are not allowed, answering as "your mom" is obnoxious")
playname = input("What is your preferred name? ")
playername = (str.lower(playname))
if playername == ("sudo su"):
    playername = "{@root}"
    pset = input("Set Points? ")
    if pset == "y":
        points = input("Points ")
        point = int(points)
        playscore = point
    dev = input("question ")
    question = int(dev)
elif [x for x in banned if x in playername]:
    print("Yeah, NO" + "  Nice try...deducting 20 points for stupidity")
    playscore -= 20
    playername = randlist[randname]
elif playername == cheatcode:
    print("Nice!")
    print("Score:")
    print("300")
    playscore += 300
    playername = ("Superuser")
    playscore -= 300
else:
    question = 0
enc = ["you can do this","never doubt yourself","good effort","almost"]
incorrectnum = random.randrange(0,3)
questions = [
    "What does the prime meridian run through",
    "what kind of orbit draws a line through a majority of the 7 wonders of the ancient world", "what is the largest metropoitan area in the world that does not body water?","The Kaliningrad oblast borders what body of water", "What country has the lowest people per square mile?","how many time zones does russia span?","what does AMS stand for","T/F:mount everest is the tallest point on earth","T/F:australia is narrower than the moon","What are citizens of Sudan called","Does Japan's traffic run on the left or right"]
answers = [
    ["england","observatory","royal observatory"],
    ["two day","two","2"], "johannesburg","baltic sea", "namibia", "11", "accelerator mass spectrometry", ["false","f"],["false","f"],"sudanese","left"
]
idkount = 0

print("Hello, " + playername)

qai1n = input(questions[question] + "? ")
if qai1n in answers[question]:
    print("correct")
    playscore += 10
    print("score:  ")
    print(playscore)
    question += 1

elif qai1n in idkbanned:
    idkount += 1
    if idkount > 3:
        print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
    elif idkount > 3:
        print(notallowed + ", " + playername)
        playscore -= 10
        question += 1
else:
    print("Wrong, but " + enc[incorrectnum])
    question += 1

qai2n = input(questions[question] + "? ")
if qai2n in answers[question]:
    print("correct")
    playscore += 10
    print("score:  ")
    print(playscore)
    question += 1

elif qai2n in idkbanned:
    idkount += 1
    if idkount > 3:
        print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
    elif idkount > 3:
        print(notallowed + ", " + playername)
        playscore -= 10
        question += 1
else:
    print("Wrong, but " + enc[incorrectnum])
    question += 1

qai3n = input(questions[question] + "? ")
if qai3n in answers[question]:
    print("correct")
    playscore += 10
    print("score:  ")
    print(playscore)
    question += 1
elif qai3n in idkbanned:
    idkount += 1
    if idkount > 3:
        print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
    elif idkount > 3:
        print(notallowed + ", " + playername)
        playscore -= 10
        question += 1
else:
    print("Wrong, but " + enc[incorrectnum])

    question += 1

qai4n = input(questions[question] + "? ")
if qai4n in answers[question]:
    print("correct")
    playscore += 10
    print("score:  ")
    print(playscore)
    question += 1
elif qai4n in idkbanned:
    idkount += 1
    if idkount > 3:
        print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
    elif idkount > 3:
        print(notallowed + ", " + playername)
        playscore -= 10
        question += 1
else:
    print("Wrong, but " + enc[incorrectnum])

    question += 1

qai5n = input(questions[question] + "? ")
if qai5n in answers[question]:
    print("correct")
    playscore += 10
    print("score:  ")
    print(playscore)
    question += 1
elif qai5n in idkbanned:
    idkount += 1
    if idkount > 3:
        print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
    elif idkount > 3:
        print(notallowed + ", " + playername)
        playscore -= 10
        question += 1
else:
    print("Wrong, but " + enc[incorrectnum])
    question += 1

qai6n = input(questions[question] + "? ")
if qai

<details>
<summary>英文:</summary>

I am using python to create a quiz for a school project. I want to have a list of names and question answers that people cannot input. (BC they are inappropriate). Whenever i use the code i have now, it says that any input is one of the blocked ones. I am using an in-browser editor(No pip installations are available to me).

This is my code. I have the list of blocked strings in a variable called &quot;Banned&quot; At first this piece of code worked, but eventually when anyone played it, the code would block all inputs, not just the ones in the Banned list.

import os
import random
print("No Capitals!")

cheatcode = ""
randlist = ["Shiny Cupcake","Good Puppy","Sunshine","Pretty Painting","Happy Trees","Soft Kitten","in love","starstuck"]
randname = random.randrange(0,6)
playscore = 0
question = 0
idkbanned = ["idk","na","n/a","urmom","ur mom","yourmom","your mom","duh"]
banned = ["ur","mom","your mom","yourmom","daddy"]
notallowed = ("Warning, Idk, N/A and duh are not allowed, answering as ""your mom"" is obnoxious")
playname = input("What is your preferred name? ")
playername = (str.lower(playname))
if playername == ("sudo su"):
playername = "{@root}"
pset = input("Set Points? ")
if pset == "y":
points = input("Points ")
point = int(points)
playscore = point
dev = input("question ")
question = int(dev)
#elif [x for x in banned if x in playername]:

print("Yeah, NO" + " Nice try...deducting 20 points for stupidity")

playscore -= 20

playername = randlist[randname]

elif playername == cheatcode:
print("Nice!")
print("Score:")
print("300")
playscore += 300
playername = ("Superuser")
playscore -= 300
else:
question = 0
enc = ["you can do this","never doubt yourself","good effort","almost"]
incorrectnum = random.randrange(0,3)
questions = [
"What does the prime meridian run through",
"what kind of orbit draws a line through a majority of the 7 wonders of the ancient world", "what is the largest metropoitan area in the world that does not body water?","The Kaliningrad oblast borders what body of water", "What country has the lowest people per square mile?","how many time zones does russia span?", "what does AMS stand for","T/F:mount everest is the tallest point on earth","T/F:australia is narrower than the moon","What are citizens of Sudan called","Does Japan's traffic run on the left or right"]
answers = [
["england","observatory","royal observatory"],
["two day","two","2"], "johannesburg","baltic sea", "namibia", "11", "accelerator mass spectrometry", ["false","f"],["false","f"],"sudanese","left"
]
idkount = 0

print("Hello, " + playername)

qai1n = input(questions[question] + "? ")
if qai1n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1

elif qai1n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1

qai2n = input(questions[question] + "? ")
if qai2n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1

elif qai2n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1

qai3n = input(questions[question] + "? ")
if qai3n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai3n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])

question += 1

qai4n = input(questions[question] + "? ")
if qai4n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai4n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])

question += 1

qai5n = input(questions[question] + "? ")
if qai5n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai5n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1

qai6n = input(questions[question] + "? ")
if qai6n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai6n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1
qai7n = input(questions[question] + "? ")
if qai7n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai7n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1
qai8n = input(questions[question] + "? ")
if qai8n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai8n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1
qai9n = input(questions[question] + "? ")
if qai9n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai9n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1
qai10n = input(questions[question] + "? ")
if qai9n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai10n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1
qai11n = input(questions[question] + "? ")
if qai11n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai11n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1
qai12n = input(questions[question] + "? ")
if qai12n in answers[question]:
print("correct")
playscore += 10
print("score: ")
print(playscore)
question += 1
elif qai12n in idkbanned:
idkount += 1
if idkount > 3:
print("No more IDK, N/A, or your mom, " + playername + " You are stalling")
elif idkount > 3:
print(notallowed + ", " + playername)
playscore -= 10
question += 1
else:
print("Wrong, but " + enc[incorrectnum])
question += 1

print("Score")
print(playscore)

This is what I get when using any name(A as an example)
Console: What is your preferred name? A Yeah, NO Nice try...deducting 20 points for stupidity Hello, Shiny Cupcake What does the prime meridian run through?
I have tried 
if playername in list(Banned)
I know that my code is not DRY.
</details>
# 答案1
**得分**: 1
取消注释:

#elif [x for x in banned if x in playername]:

print("Yeah, NO" + " Nice try...deducting 20 points for stupidity")

playscore -= 20

playername = randlist[randname]

并且你会修复它。
<details>
<summary>英文:</summary>
Uncomment:

#elif [x for x in banned if x in playername]:

print("Yeah, NO" + " Nice try...deducting 20 points for stupidity")

playscore -= 20

playername = randlist[randname]

And you&#39;ll have fixed it.
</details>
# 答案2
**得分**: 1
你说得对,“in”关键字是你要找的,但你的代码顺序不是最佳的。
```python
banned = ["ur", "mom", "your mom", "yourmom"]
playername = input("你的名字")
if playername in banned:
print("好尝试")
else:
print("欢迎 " + playername)

你可能还想创建一个方法,以避免反复进行相同的验证。阻止游戏继续进行的一种好方法是引发异常,你可能想检查一下这个概念,但这将停止执行。

def checkProfanity(word):
if word in banned:
  raise Exception("禁止咒骂")

除此之外,这不是检查输入中字符串存在的最佳方法。

for word in banned:
   if playername.__contains__(word):
      print("好尝试")

这也会过滤掉像 'xxx mom xxx' 这样的内容。

英文:

You're correct the in keywork is what you're looking for but your code is not in the best order

banned = [&quot;ur&quot;,&quot;mom&quot;,&quot;your mom&quot;,&quot;yourmom&quot;]
playername = input(&quot;your name&quot;)
if playername in banned:
print(&quot;good try&quot;)
else:
print(&quot;welcome &quot;+playername)

you might also want to create a method to avoid repeating the same verification over and over again. A good way to stop the game from continuing is to raise an exception you migth want to check that concept but this will stop the execution

def checkProfanity(word):
if word in banned:
raise Exception(&quot;No cursing&quot;)

Besides that this is not the best way to check for the presence of a string in the input.

for word in banned:
if playername.__contains__(word):
print(&quot;good try&quot;)

will also filter things like 'xxx mom xxx'

huangapple
  • 本文由 发表于 2023年2月16日 10:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定