如何使用两个for循环随机替换句子中的多个单词(How to use two for loops to randomly replace multiple words in a sentence)
我的任务是创建一个随机补语生成器,它使用两个for循环来随机选择和替换句子中的名词和形容词。 我应该使用random.choice功能来选择名词/形容词,然后用替换物打印出这个句子。 我在下面做了大部分工作,但由于某种原因,程序只是替换名词而不是形容词。 相反,它会在占位符后打印出想要的形容词。
import random sentence = "Hi, your *noun is looking *adj today." sentence = sentence.split() nouns = ["son", "daughter", "dog", "house"] adjectives = ["beautiful", "handsome", "pretty", "warm", "fantastic"] indexCount = 0 for word in sentence: if word == "*noun": wordChoice = random.choice(nouns) sentence[indexCount] = wordChoice indexCount += 1 if word == "*adj": wordChoice = random.choice(adjectives) sentence[indexCount] = wordChoice st = "" for word in sentence: st += word + " " print(st)
我在IDLE中得到的输出类似于:
Hi, your son is looking *adj warm
今天根本不打印出来。 当我尝试添加第二个indexCount + = 1时,程序返回它超出范围。
My assignment was to create a random compliment generator that uses two for loops to randomly select and replace both a noun and an adjective in a sentence. I am supposed to utilize the random.choice function to select the noun/adj and then print out the sentence with the replacements. I've done most of it below but for some reason, the program is only replacing the noun but not the adjective. Instead, it's printing out the intended adjective after the placeholder.
import random sentence = "Hi, your *noun is looking *adj today." sentence = sentence.split() nouns = ["son", "daughter", "dog", "house"] adjectives = ["beautiful", "handsome", "pretty", "warm", "fantastic"] indexCount = 0 for word in sentence: if word == "*noun": wordChoice = random.choice(nouns) sentence[indexCount] = wordChoice indexCount += 1 if word == "*adj": wordChoice = random.choice(adjectives) sentence[indexCount] = wordChoice st = "" for word in sentence: st += word + " " print(st)
The output I keep getting in IDLE is similar to:
Hi, your son is looking *adj warm
The today isn't printed out at all. When I try to add a second indexCount += 1, the program returns that it's out of range.
原文:https://stackoverflow.com/questions/39969251
最满意答案
只需自己构建URL,
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fgoogle.com%2F" scrolling="no" frameborder="0" style="height: 62px; width: 100%" allowTransparency="true"></iframe>
并将您网站的URL urlencoded放在
?href=
后面。Just construct the URL yourself,
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fgoogle.com%2F" scrolling="no" frameborder="0" style="height: 62px; width: 100%" allowTransparency="true"></iframe>
and put your site's URL urlencoded behind the
?href=
.
相关问答
更多-
要回答你的问题,考虑按钮会发生什么,然后进入像按钮。 按下按钮会触发客户端上的事件,该事件可能会或可能不会在某处更新服务器,以通知按钮已被按下以实现此目的。 您可以通过此活动发送大量额外信息,例如何时何地以及为什么等 像按钮通常有额外的信息谁喜欢它,他们喜欢什么。 为了得到你可能会要求人们登录或提供某种输入来识别它们。 采用一个真实世界的例子,您可以使用任何服务器端技术在JavaScript中实现类似的按钮 谁安装你的脚本将能够看到按钮。 你可以使用任何CSS来构建它,或者你的JavaScript可以简单地 ...
-
我的提交按钮是如何自动标记为“创建用户”的(How did my submit button automatically label itself as “Create User”)[2022-03-21]
对于创建操作,表单提交按钮的Rails约定具有“创建”形式。 您可以通过在提交后添加字符串来覆盖它。 例如: f.submit "Sign up" For a create action, it's a Rails convention for the form submit button to have the form "Create ". You an override this by adding a string after submit. For example: f.submit "Sign ... -
对于无法动态创建Facebook应用/用户/组和页面的假设,您是正确的。 什么是woo box是许多其他页面选项卡应用程序正在做的事情。 他们为不同的模板创建了许多应用程序,然后根据您的输入进行自定义。 他们不会动态创建应用程序。 这些动态应用程序从app_data字段获取所需数据。 另一点需要注意的是 应用无法在页面上安装两次 这就是为什么许多提供页面选项卡应用程序的服务提供商维护一个应用程序池以满足其用户的需求。 You are correct with the presumptions that Fa ...
-
像Facebook页面按钮(Like Facebook Page button)[2022-04-24]
这更像吗? https://developers.facebook.com/docs/reference/plugins/like/ 所以你的代码看起来像这样: 的iFrame: