首页 \ 问答 \ 如何使用两个for循环随机替换句子中的多个单词(How to use two for loops to randomly replace multiple words in a sentence)

如何使用两个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
更新时间:2023-01-03 22:01

最满意答案

只需自己构建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可以简单地 ...
  • 对于创建操作,表单提交按钮的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 ...
  • 这更像吗? https://developers.facebook.com/docs/reference/plugins/like/ 所以你的代码看起来像这样: 的iFrame: 并将您网站的URL urlencoded放在?href=后面。 Just construct the ...
  • 没有办法做到这一点。 基本上,如果用户按下“喜欢”按钮,则会将“用户喜欢[链接]”的短消息发布到用户的墙上。 并且,如果您对类似按钮使用XFBML或HTML5版本代码,则会向用户显示文本输入对话框,以便将其发布到墙上。 就这样。 There is no way to do that. Basically, If a user push the like button, a short message that says 'a user liked [link]' will be posted to the ...
  • 使用Facebook提供的代码生成器: https://developers.facebook.com/docs/facebook-login/web/login-button 请注意,为了使用本教程中演示的Facebook SDK onlogin函数,您需要添加自定义属性来链接功能,例如:

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)