显然我犯了很多错误,我是Python的新手,所以这就是原因。
我在一个random.randint
语句下有一个if
,另一个在elif
语句下。但是,即使我将输入内容输入到应该使我进入第二答案的答案中,也会出现第一答案。
这是我的代码:
import random
LOL = raw_input('Do you want a meme or a prank idea? ')
if LOL == 'meme' or 'Meme' or 'MEME':
x = random.randint(1, 5)
if x == 1:
print('Yee')
elif x == 2:
print('Yeet!')
elif x == 3:
print('I got the horses in the back')
elif x == 4:
print('AND HIS NAME IS JOHN CENA!!!!')
elif x == 5:
print('IT\'S OVER 9000!')
elif LOL == 'prank' or 'Prank' or 'PRANK':
y = random.randint(1, 3)
if y == 1:
print('Replace their Oreo frosting with toothpaste.')
elif y == 2:
print('Blast into their room with an air horn.')
elif y == 3:
print('Blast the FBI meme on a loud speaker on max volume')
即使我写了“恶作剧”,它也显示了一个模因,我认为这是因为random.randint
无论如何都会走,而且因为它是第一个,所以它会先出现并与第二个random.randint
重叠。
答案 0 :(得分:0)
您使用或操作员的方式错误
尝试一下:
import random
LOL = raw_input('Do you want a meme or a prank idea? ')
if LOL == 'meme' or LOL == 'Meme' or LOL == 'MEME':
x = random.randint(1, 5)
if x == 1:
print('Yee')
elif x == 2:
print('Yeet!')
elif x == 3:
print('I got the horses in the back')
elif x == 4:
print('AND HIS NAME IS JOHN CENA!!!!')
elif x == 5:
print('IT\'S OVER 9000!')
elif LOL == 'prank' or LOL == 'Prank' or LOL == 'PRANK':
y = random.randint(1, 3)
if y == 1:
print('Replace their Oreo frosting with toothpaste.')
elif y == 2:
print('Blast into their room with an air horn.')
elif y == 3:
print('Blast the FBI meme on a loud speaker on max volume')
输出
Do you want a meme or a prank idea? prank
Blast the FBI meme on a loud speaker on max volume
答案 1 :(得分:0)
猜猜random.choice
在这里更合适吗? =)
import random
LOL = input("Do you want a meme or a prank idea? ")
prank_or_meme = {
"meme": [
"Yee",
"Yeet!",
"I got the horses in the back",
"AND HIS NAME IS JOHN CENA!!!!",
"IT'S OVER 9000!",
],
"prank": [
"Replace their Oreo frosting with toothpaste.",
"Blast into their room with an air horn.",
"Blast the FBI meme on a loud speaker on max volume",
],
}
default = ["Philosoraptor: I don't know, may be it\'s a joke?"]
values = prank_or_meme.get(LOL.lower(), default)
print(random.choice(values))
答案 2 :(得分:0)
首先,您没有正确使用or
,您需要这样做:
if LOL == 'Meme' or LOL == 'meme' or LOL == "MEME":
您应改为使用名为lower
或upper
的方法,如下所示:
if LOL.lower() == 'meme':
或者这个:
if LOL.upper() == 'MEME':
第二,您的缩进没有意义,我会猜测您的意思:
import random
LOL = raw_input('Do you want a meme or a prank idea? ')
if LOL == 'meme' or 'Meme' or 'MEME':
x = random.randint(1, 5)
if x == 1:
print('Yee')
elif x == 2:
print('Yeet!')
elif x == 3:
print('I got the horses in the back')
elif x == 4:
print('AND HIS NAME IS JOHN CENA!!!!')
elif x == 5:
print('IT\'S OVER 9000!')
elif LOL == 'prank' or 'Prank' or 'PRANK':
y = random.randint(1, 3)
if y == 1:
print('Replace their Oreo frosting with toothpaste.')
elif y == 2:
print('Blast into their room with an air horn.')
elif y == 3:
print('Blast the FBI meme on a loud speaker on max volume')
为什么您不工作?原因if
收到一个布尔值,并且文本字符串像一个真实值一样被读取,所以当您这样做时:
if 'Im a string':
print('I will be printed')
将打印文本。尝试使用我的上述建议。 @John Coleman和@jonrsharpe是正确的。