我试图在第一天和第二天从电影列表中打印两个唯一的字符串。到目前为止,我已经尝试了random.choice,但后来我切换到了random.shuffle。我有两部电影放映,但两天都在播出,我不确定将这两部电影分开的步骤。
from flask import Flask
import random
app = Flask(__name__)
app.config['DEBUG'] = True # displays runtime errors in the browser, too
@app.route("/")
def index():
# choose a movie by invoking our new function
movie = get_random_movie()
# build the response string
day1 = "<h1>Movie of the Day</h1>"
day1 += "<ul>"
day1 += "<li>" + movie + "</li>"
day1 += "</ul>"
# build the response string
day2 = "<h1>Movie of the Day Tomorrow</h1>"
day2 += "<ul>"
day2 += "<li>" + movie + "</li>"
day2 += "</ul>"
return day1 + day2
def get_random_movie():
movies = ["Akira", "Ghost In The Shell", "Princess Mononoke", "Kimi no na wa", "Howl's Moving Castle" ]
random.shuffle(movies)
return movies[1] + movies[2]
app.run()
这是我的输出
每日电影
哈尔的移动城堡阿基拉
今日电影明天
哈尔的移动城堡阿基拉
答案 0 :(得分:0)
您可以将值封装为列表或使用列表切片:
[movies[1]] + [movies[2]]
或者:
movies[1:3]
答案 1 :(得分:0)
你正在连接两个字符串,你想要的是从你的函数返回一个元组然后正确地分配值
from flask import Flask
import random
app = Flask(__name__)
app.config['DEBUG'] = True # displays runtime errors in the browser, too
@app.route("/")
def index():
# choose a movie by invoking our new function
movie = get_random_movie()
# build the response string
day1 = "<h1>Movie of the Day</h1>"
day1 += "<ul>"
day1 += "<li>" + movie[0] + "</li>"
day1 += "</ul>"
# build the response string
day2 = "<h1>Movie of the Day Tomorrow</h1>"
day2 += "<ul>"
day2 += "<li>" + movie[1] + "</li>"
day2 += "</ul>"
return day1 + day2
def get_random_movie():
movies = ["Akira", "Ghost In The Shell", "Princess Mononoke", "Kimi no na wa", "Howl's Moving Castle" ]
random.shuffle(movies)
return (movies[1], movies[2])
app.run()