更新:感谢非常善良的人,我现在正确地(?)缩进了代码!
在你投票或无视之前,请帮忙!我对Python不是很熟练,我查看了一些与我的问题相关的帖子。
对于学校项目,我需要创建三个Mad Libs故事并确保它们是随机选择的。我根本不明白如何让这些故事随机发生!我已经尝试使用import random和random.choice等类似的东西,但它只是不起作用!此外,在我试验这个时,我的疯狂的自由人现在不会按照我的意愿运行!
任何帮助将不胜感激!
print("Welcome to my terrible Mad Libs stories. It is not a smart idea to run through my stories. In fact, it is much more wise for you to do something else.")
def story1 ():
proper_name = input("Why are you doing this. Enter a name: ")
adjective1 = input("Seriously. Stop. Enter an adjective: ")
adjective2 = input("Enter an adjective: ")
adjective3 = input("Enter an adjective: ")
adjective4 = input("ADJECTIVES. Enter an adjective: ")
adjective5 = input("Enter an adjective: ")
adjective6 = input("Why are there so many adjectives... Enter an adjective: ")
city = input("Enter a city name: ")
color = input("Enter a color: ")
plural_clothing = input("Enter a plural article of clothing: ")
plural_noun1 = input("Enter a plural noun: ")
plural_noun2 = input("Last one! Enter a plural noun: ")
plural_noun3 = input("HAH JUST KIDDING. Last one. Enter a plural noun: ")
plural_noun4 = input("AHAHAHA THAT WASN'T THE LAST ONE...Enter a plural noun: ")
print("{} has announced that his {} clothing "
"store in the heart of downtown {}, is having a {} "
"sale of all merchandise, including {} suits "
"and slightly irregular {}. Men's cable-knit "
"{}, only $15.99. Hand-woven Italian {}, half-price. "
"Double-breasted cashmere {}, $50.00. Genuine imported "
" {} {} shoes, {} handkerchiefs, "
"and women's embroidered {}, all at rock-bottom prices. "
"This is a chance to get some really {} bargains!".format(
proper_name, adjective1, city, adjective2, adjective3, plural_clothing, plural_noun1, plural_noun2, plural_noun3, color, adjective4, adjective5, plural_noun4, adjective6))
def story2 ():
proper_name = input("Enter a name: ")
disease = input("Enter a disease: ")
body_part = input("Enter a bodypart: ")
exclamation = input("Enter an exclamation: ")
adjective = input("Enter an adjective: ")
nasty_adjective = input("Enter a nasty adjective: ")
noun = input("Enter a noun: ")
plural_noun1 = input("You're still here? You should quit... enter a plural noun: ")
plural_noun2 = input("Enter another plural noun: ")
plural_noun3 = input("Almost done... enter another plural noun: ")
plural_noun4 = input("One more! Enter another plural noun: ")
print("Medical science and people, but mainly science, have discovered "
"that smoking cigarettes causes {} (obviously). It is also "
"bad for your {} and causes pains in the {} "
"(bet you didn't know that). When mice and cats were exposed to "
"{} cigarette smoke, they developed {} disease (gasp!). "
"Tobacco companies have put charcoal {} on the ends of cigarettes, "
"but they still spend millions of {} advertising their {} "
"product (gross). If you smoke cigarettes, the tobacco will leave all "
"kinds of tar and {} in your lungs. This will make you cough "
"and say, '{}!' Don't smoke cigarettes. Remember, only {} smoke.".format(
disease, noun, body_part, adjective, proper_name, plural_noun1, plural_noun2, nasty_adjective, plural_noun3, exclamation, plural_noun4))
def story3():
country = input("I love Mad Libs. Enter a geographic location: ")
adjective1 = input("The line before this one was false. Enter an adjective: ")
adjective2 = input("Enter an adjective: ")
clothing = input("Enter an article of clothing: ")
number1 = input("Enter a number: ")
number2 = input("Well, I guess you should enter another number: ")
plural_noun1 = input("How about a plural noun?: ")
plural_noun2 = input("Wait, you need another plural noun: ")
print("Here is tomorrow's weather report for AMERICA "
"and vicinity. Early tomorrow, a HAPPY front will "
"collide with a mass of hot PANTS moving from the "
"north. This means we can expect NICE winds and "
"occasional BIRDS by late afternoon. Wind velocity will "
"be 23 miles an hour, and the high temperature should "
"be around 12 degrees. So, if you're going out, you had "
"better plan on wearing your SHIRT. ".format(
country, adjective1, plural_noun1, adjective2, plural_noun2, number1, number2, clothing))
print((story1), (story2), (story3))
import random
story = ['story1', 'story2', 'story3']
print(random.choice(story))
答案 0 :(得分:0)
===============修订=================
对于您的原始代码,我认为问题主要发生在这里:
print((story1), (story2), (story3))
调用3个函数,但没有记录,并且它没有以正确的方式调用,你应该:
print(story1(), story2(), story3())
可以调用这些函数(只是正确的格式,不能解决你的问题)。
并且:
import random
story = ['story1', 'story2', 'story3']
print(random.choice(story))
问题是'story1'
是一个字符串,因此只会随机填充3个字符串'story1'
,'story2'
和'story3'
。而且,对于每个函数,您只需print()
加入的故事,因此一旦您离开该函数,由于变量范围,该字符串将不再存在。所以我做了一些改变:
print("Welcome to my terrible Mad Libs stories. It is not a smart idea to run through my stories. In fact, it is much more wise for you to do something else.")
def story1 ():
proper_name = input("Why are you doing this. Enter a name: ")
adjective1 = input("Seriously. Stop. Enter an adjective: ")
adjective2 = input("Enter an adjective: ")
adjective3 = input("Enter an adjective: ")
adjective4 = input("ADJECTIVES. Enter an adjective: ")
adjective5 = input("Enter an adjective: ")
adjective6 = input("Why are there so many adjectives... Enter an adjective: ")
city = input("Enter a city name: ")
color = input("Enter a color: ")
plural_clothing = input("Enter a plural article of clothing: ")
plural_noun1 = input("Enter a plural noun: ")
plural_noun2 = input("Last one! Enter a plural noun: ")
plural_noun3 = input("HAH JUST KIDDING. Last one. Enter a plural noun: ")
plural_noun4 = input("AHAHAHA THAT WASN'T THE LAST ONE...Enter a plural noun: ")
return '''{} has announced that his {} clothing
store in the heart of downtown {}, is having a {}
sale of all merchandise, including {} suits
and slightly irregular {}. Men's cable-knit {},
only $15.99. Hand-woven Italian {}, half-price.
Double-breasted cashmere {}, $50.00. Genuine imported {}
{} shoes, {} handkerchiefs, and women's embroidered {},
all at rock-bottom prices. This is a chance to get
some really {} bargains!'''.format(proper_name,\
adjective1, city, adjective2, adjective3, \
plural_clothing, plural_noun1, plural_noun2, \
plural_noun3, color, adjective4, adjective5, \
plural_noun4, adjective6)
def story2 ():
proper_name = input("Enter a name: ")
disease = input("Enter a disease: ")
body_part = input("Enter a bodypart: ")
exclamation = input("Enter an exclamation: ")
adjective = input("Enter an adjective: ")
nasty_adjective = input("Enter a nasty adjective: ")
noun = input("Enter a noun: ")
plural_noun1 = input("You're still here? You should quit... enter a plural noun: ")
plural_noun2 = input("Enter another plural noun: ")
plural_noun3 = input("Almost done... enter another plural noun: ")
plural_noun4 = input("One more! Enter another plural noun: ")
return '''Medical science and people, but mainly science,
have discovered that smoking cigarettes causes {}
(obviously). It is also bad for your {}
and causes pains in the {} (bet you didn't know that).
When mice and cats were exposed to {} cigarette smoke,
they developed {} disease (gasp!). Tobacco companies
have put charcoal {} on the ends of cigarettes,
but they still spend millions of {} advertising
their {} product (gross). If you smoke cigarettes,
the tobacco will leave all kinds of tar and {}
in your lungs. This will make you cough and say,
'{}!' Don't smoke cigarettes. Remember, only {}
smoke.'''.format(disease,noun, body_part, adjective,\
proper_name, plural_noun1, plural_noun2, nasty_adjective, \
plural_noun3, exclamation, plural_noun4)
def story3():
country = input("I love Mad Libs. Enter a geographic location: ")
adjective1 = input("The line before this one was false. Enter an adjective: ")
adjective2 = input("Enter an adjective: ")
clothing = input("Enter an article of clothing: ")
number1 = input("Enter a number: ")
number2 = input("Well, I guess you should enter another number: ")
plural_noun1 = input("How about a plural noun?: ")
plural_noun2 = input("Wait, you need another plural noun: ")
#I don't know where youre gonna put these words...
return '''{}{}{}{}{}{}{}{}Here is tomorrow's weather report for
AMERICA and vicinity. Early tomorrow, a HAPPY front
will collide with a mass of hot PANTS moving
from the north. This means we can expect NICE
winds and occasional BIRDS by late afternoon.
Wind velocity will be 23 miles an hour, and the
high temperature should be around 12 degrees.
So, if you're going out, you had better plan on
wearing your SHIRT.'''.format(country, adjective1, \
plural_noun1, adjective2, plural_noun2, number1, \
number2, clothing)
storyOne = story1().replace('\n','').replace(' ','')
storyTwo = story2().replace('\n','').replace(' ','')
storyThree = story3().replace('\n','').replace(' ','')
import random
story = [storyOne, storyTwo, storyThree]
print(random.choice(story))
现在每个函数都返回一个字符串,并使用storyThree = story3().replace('\n','').replace(' ','')
来记录该字符串,因此storyOne
,storyTwo
和storyThree
是3个变量,因此它可以放入列表,然后random.choice(story)
将起作用。