随机字符串作为输出

时间:2017-02-02 17:09:43

标签: python for-loop

我正在尝试编写从列表中抽取随机同义词的代码。 而不是那样,我得到一个随机的字符串,似乎与我的任何代码无关。

这是主模块代码:

<?php
<div class="modal fade" id="albumModal1">
<div class="modal-dialog">
<div class="modal-content">
    <!-- Top right close X -->
    <div class="close-modal" data-dismiss="modal">
        <span  class="glyphicon glyphicon-remove"</span>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2">
                <div class="modal-body">
                    <p class="modalTitle">The Beatles: Abby Road</p>
                    <img src="beatles.jpg" class="img-responsive center-block albumImgGrey">
                    <!-- Album 1's Review -->
                    <div class="modalText">
                        <p>Content upon content upon more content 
                            <p>upon more content</p>
                            Content upon content upon more content 
                            <p>upon more content</p>
                            <div class="starcolor">
                                <span>&#9733 &#9733 &#9733 &#9733 &#9733</span>
                            </div>
                        </p>
                        <!-- PHP content here....... -->
                        $id = $_GET['id'];
                        $link = mysql_connect("localhost", "root", "");
                        mysql_select_db("images");
                        $sql = "SELECT review FROM reviews WHERE id=$id";
                        $result = mysql_query("$sql");
                        $row = mysql_fetch_assoc($result);
                        mysql_close($link);
                        echo $row['review'];
                        <!-- PHP content here....... -->
                        <!-- Bottom of the review links -->
                        <ul class="list-inline item-details">
                            <li> 
                                Year of release: <strong><a href="#">3000</a></strong>
                            </li>
                            <li>
                                Previous Album: <strong><a href="#">Hippie tree</a></strong>
                            </li>
                            <li>
                                Following Album: <strong><a href="#">Backup Plus++</a></strong>
                            </li>
                        </ul>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
?>

这是respond.py的代码:

from output import *
import definitions
from responses import *

…

def respond(wordList):
    output = ""
    for word in wordList:
        output = (output + " " + (random.choice(word)))
    return output

def edison():
    mood = ask("Hi, " + username + "! How are you today? ")
    if mood.lower() in definitions.positive:
        print(respond(['i_am', 'happy', 'to' 'hear', 'that']) + "!")
    elif mood.lower() in definitions.negative:
        print(respond(['i_am', 'sorry_unhappy', 'to' 'hear', 'that']) + "!")

…

edison()

以下是我的输出示例:

i_am = ["I am", "I'm"]
happy = ["cheerful", "delighted", "glad", "joyful", "joyous", "overjoyed", "pleased", "thrilled", "gleeful", "happy"]
sorry_unhappy = ["sorry"]
to = ["to"]
hear = ["listen to", "hear"]
that = ["that"]

2 个答案:

答案 0 :(得分:3)

问题可能是random.choice(word)。 word是wordList的元素,从字符串中随机选择会选择一个随机字母。请改为random.choice(wordList)

如果你想将wordList连接为输出,因为它看起来已经像某个句子,你可以这样做:

output = " ".join(wordList)

答案 1 :(得分:0)

您没有使用来自`responses.py'的内置响应,而是使用固定的str响应

edison替换为:

def edison():
    mood = ask("Hi, " + username + "! How are you today? ")
    if mood.lower() in definitions.positive:
        print(respond([i_am, happy, to, hear, that]) + "!")
    elif mood.lower() in definitions.negative:
       print(respond([i_am, sorry_unhappy, to, hear, that]) + "!")