如何选择Python中的随机对话输出?
我试图让我的脚本在三种对话选项中进行选择,分别标记为PetConval1、PetConval2或PetConval3。该程序询问用户有什么宠物,然后随机挑选一件关于他们的话。根据脚本选择的花絮,我想继续那里的对话。我的问题是,它只是停止了,不能继续进行对话。最好的方法是什么 我的代码如下如何选择Python中的随机对话输出?,python,if-statement,random,Python,If Statement,Random,我试图让我的脚本在三种对话选项中进行选择,分别标记为PetConval1、PetConval2或PetConval3。该程序询问用户有什么宠物,然后随机挑选一件关于他们的话。根据脚本选择的花絮,我想继续那里的对话。我的问题是,它只是停止了,不能继续进行对话。最好的方法是什么 我的代码如下 #imports the random module import random #Asks what pet the user has pet = input("What kind of pet d
#imports the random module
import random
#Asks what pet the user has
pet = input("What kind of pet do you have? ")
#Sets the input to lowercase
petlower = pet.lower()
#Sets tidbits of conversation about the pet
petconvo1 = ("Oh, I've heard that " + petlower + " is a good companion to have.")
petconvo2 = ("Oh, really? I had " + petlower + " when I was growing up.")
petconvo3 = ("Does it cost a lot to keep " + petlower + " fed?")
#Turns the tidbits into a list
petconvolist = [petconvo1, petconvo2, petconvo3]
#Compliments pet
print(random.choice(petconvolist))
#Continues the conversation
if petconvolist == petconvo1:
time.sleep(1)
print("Wouldn't you agree?")
else:
if petconvolist == petconvo2:
time.sleep(1)
print("I think it's important for children to grow up with pets.")
else:
if petconvolist == petconvo3:
time.sleep(1)
foodbudget=input("Does it cost a lot to feed " + petlower + "?")
time.sleep(.5)
else: pass
脚本在此位之后停止:
print(random.choice(petconvolist))
提前谢谢 hmm在if语句中,首先应该使用elif语句。之后,您应该使用将随机选择设置为var来检查它。这个代码对我有用
#imports the random module
import random
#Asks what pet the user has
pet = input("What kind of pet do you have? ")
#Sets the input to lowercase
petlower = pet.lower()
#Sets tidbits of conversation about the pet
petconvo1 = ("Oh, I've heard that " + petlower + " is a good companion to have.")
petconvo2 = ("Oh, really? I had " + petlower + " when I was growing up.")
petconvo3 = ("Does it cost a lot to keep " + petlower + " fed?")
#Turns the tidbits into a list
petconvolist = [petconvo1, petconvo2, petconvo3]
#Compliments pet
convo = random.choice(petconvolist)
print(convo)
#Continues the conversation
if convo == petconvo1:
time.sleep(1)
print("Wouldn't you agree?")
elif convo == petconvo2:
time.sleep(1)
print("I think it's important for children to grow up with pets.")
elif convo == petconvo3:
time.sleep(1)
foodbudget=input("Does it cost a lot to feed " + petlower + "?")
time.sleep(.5)
嗯,首先在if语句中,您应该使用elif语句。之后,您应该使用将随机选择设置为var来检查它。这个代码对我有用
#imports the random module
import random
#Asks what pet the user has
pet = input("What kind of pet do you have? ")
#Sets the input to lowercase
petlower = pet.lower()
#Sets tidbits of conversation about the pet
petconvo1 = ("Oh, I've heard that " + petlower + " is a good companion to have.")
petconvo2 = ("Oh, really? I had " + petlower + " when I was growing up.")
petconvo3 = ("Does it cost a lot to keep " + petlower + " fed?")
#Turns the tidbits into a list
petconvolist = [petconvo1, petconvo2, petconvo3]
#Compliments pet
convo = random.choice(petconvolist)
print(convo)
#Continues the conversation
if convo == petconvo1:
time.sleep(1)
print("Wouldn't you agree?")
elif convo == petconvo2:
time.sleep(1)
print("I think it's important for children to grow up with pets.")
elif convo == petconvo3:
time.sleep(1)
foodbudget=input("Does it cost a lot to feed " + petlower + "?")
time.sleep(.5)
您需要将随机选择存储在变量中,并在
if
statements@Aziz哦,明白了。谢谢您需要将随机选择存储在变量中,并在if
statements@Aziz哦,明白了。谢谢太好了@尼克·特姆。啊,太好了。非常感谢!太好了@尼克·特姆。啊,太好了。非常感谢!