我有 2 个 random.randint 值,如何让第二个值出现?
I have 2 random.randint values, how do I make the 2nd one come up?
显然我犯了很多错误,我对 Python 还很陌生,所以这就是原因。
我在 if
语句中有一个 random.randint
,在 elif
语句中有另一个。但是即使我将我的输入输入到应该带我到第二个答案的答案中,第一个也会出现(例如 prank
)。
这是我的代码:
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')
它甚至在我写 "prank" 时也显示了模因之一,我认为这是因为 random.randint
无论如何都会出现,因为它是第一个,所以它先出现并与第二个重叠 random.randint
.
您使用 or 运算符的方式有误
试试这个:
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
猜猜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))
首先,你没有正确使用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 是对的。
显然我犯了很多错误,我对 Python 还很陌生,所以这就是原因。
我在 if
语句中有一个 random.randint
,在 elif
语句中有另一个。但是即使我将我的输入输入到应该带我到第二个答案的答案中,第一个也会出现(例如 prank
)。
这是我的代码:
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')
它甚至在我写 "prank" 时也显示了模因之一,我认为这是因为 random.randint
无论如何都会出现,因为它是第一个,所以它先出现并与第二个重叠 random.randint
.
您使用 or 运算符的方式有误
试试这个:
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
猜猜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))
首先,你没有正确使用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 是对的。