我正在尝试创建一个函数来打印已定义数字列表的方差:
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已尝试继续制作这三个功能:
def grades_sum(my_list):
total = 0
for grade in my_list:
total += grade
return total
def grades_average(my_list):
sum_of_grades = grades_sum(my_list)
average = sum_of_grades / len(my_list)
return average
def grades_variance(my_list, average):
variance = 0
for i in my_list:
variance += (average - my_list[i]) ** 2
return variance / len(my_list)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行代码时,它会在以下行中给出以下错误:
Line: variance += (average - my_list[i]) ** 2
Error: list index out of range
Run Code Online (Sandbox Code Playgroud)
抱歉,如果我目前的Python知识有限,但我仍在学习 …
我有这个示例代码:
$array = array(
'GiamPy' => array(
'Age' => '18',
'Password' => array(
'password' => '1234',
'salt' => 'abcd',
'hash' => 'whirlpool'
),
'Something' => 'Else'
)
);
echo json_encode($array, JSON_PRETTY_PRINT);
Run Code Online (Sandbox Code Playgroud)
我在PHP文档中看到,自PHP 5.5.0(最近),json_encode允许一个新的参数,这是深度.
如何在Algolia中实现类似于亚马逊或谷歌的基于关键字的自动填充建议?
我尝试根据Algolia文档的多个属性构建自动完成,但是它的目的并不能帮助我完成一个短语,但它指导我选择一个特定的产品.
我已经构建了一个命令来触发通过互联网下载文件,但是由于这些文件需要由另一个组件处理,因此我们需要确保在过去 10 秒内已下载且未修改的每个文件都是正确的视频且未损坏/部分下载。
因此,我们需要找到一种方法来捕获 CTRL+C 或命令终止并清理任何尚未成功下载的适用文件。
这是我到目前为止通过使用symfony/consoleand尝试过的symfony/event-dispatcher:
#!/usr/bin/env php
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use ImportExport\Console\ImportCommand;
use Monolog\Logger;
$dotenv = new Dotenv\Dotenv(__DIR__ . '/../');
$dotenv->load();
$logger = new Logger('console');
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) {
// gets the command that has been executed
$command = $event->getCommand();
var_dump($command);
});
$application = new Application("Import-Export System", 'v0.1.0-ALPHA');
$application->add(new ImportCommand($logger));
$application->setDispatcher($dispatcher);
$application->run();
Run Code Online (Sandbox Code Playgroud)
但是,var_dump()如果我执行 CTRL+C,则永远不会在控制台中显示。
建议?
我正在尝试开发一个单页面应用程序(SPA),它使用一个与SPA域中托管的域不同的域作为端点(即:site.com和site-api.comor api.site.com).
访问控制标头已在后端设置,Max-Age包括在内,但它似乎不起作用.
以下是我多次执行相同调用时会发生什么的示例:
这些是服务器标头:
AUTHORIZATION,CONTENT-TYPEPATCHhttp://tovertaal.test:3000600第一次请求600秒内不应该Max-Age600防止其他所有请求吗?OPTIONSOPTIONS
服务器端点是http://tovertaal-api.test.
有许多类似的问题,但是这有点不同,因为它涉及深层对象属性访问,而不仅仅是一个深度级别.
假设我有一个包含字符串的变量foo.bar.
$user = new User();
$user->foo = new Foo();
$user->foo->bar = "Hello World";
$variable = "foo.bar"
Run Code Online (Sandbox Code Playgroud)
我想响应$user->foo->bar通过利用$variable:
echo $user->foo->bar
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止尝试但没有成功(它说NULL):
$value = str_replace(".", "->", $value);
echo $user->{$value};
Run Code Online (Sandbox Code Playgroud) 我试图通过使用一个简单的服务来抽象我的 API 调用,该服务提供了一个非常简单的方法,这只是一个 HTTP 调用。我将此实现存储在 React Context 中,并在 my 中使用其提供程序_app.js,以便该 API 全局可用,但我在实际使用页面中的上下文时遇到问题。
页面/_app.js
import React from 'react'
import App, { Container } from 'next/app'
import ApiProvider from '../Providers/ApiProvider';
import getConfig from 'next/config'
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
export default class Webshop extends App
{
static async getInitialProps({ Component, router, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render () {
const { Component, pageProps …Run Code Online (Sandbox Code Playgroud) 我想知道这两段代码之间有什么区别:
while choice != "y" and choice != "n":
while not choice == "y" and not choice == "n":
Run Code Online (Sandbox Code Playgroud) 考虑以下场景:
import React, { Component } from 'react';
import LocaleService from '../Services/LocaleService.js';
const defaultStore = {
loaded: false,
locales: []
};
const LocalesContext = React.createContext(defaultStore);
class LocalesProvider extends Component
{
state = defaultStore;
load() {
const service = new LocaleService(), that = this;
service.fetch().then(function (locales) {
that.setState({ locales: locales, loaded: true });
});
}
data() {
return this.state;
}
componentDidMount() {
this.load();
}
render() {
return (
<LocalesContext.Provider value={this.data()}>
{this.props.children}
</LocalesContext.Provider>
);
}
}
export default LocalesProvider;
Run Code Online (Sandbox Code Playgroud)
import React, { …Run Code Online (Sandbox Code Playgroud) 我正在进行登录功能,但遇到了一个我无法弄清楚的错误.
这是我的Model Login类:
class Login {
private $username;
private $password;
private $cxn; //database object
function __construct($username,$password)
{
//set data
$this->setData($username, $password);
//connect DB
$this->connectToDB();
// get Data
}
function setData($username, $password)
{
$this->username = $username;
$this->password = $password;
}
private function connectToDB()
{
include 'Database.php';
$connect = '../include/connect.php';
$this->cxn = new database($connect);
}
function getData()
{
$query = "SELECT * FROM anvandare WHERE anvandarnamn = '$this->username' AND losenord ='$this->'password'";
$sql = mysql_query($query);
if(mysql_num_rows($sql)>0)
{
return true;
}
else
{
throw …Run Code Online (Sandbox Code Playgroud) 这是响应:
[
{
"data":{
"locales":{
"translate":[
{
"created_at":"2018-05-28 12:49:53",
"deleted_at":null,
"id":1,
"key":"nl_NL",
"name":"Netherlands (Nederlands)",
"updated_at":"2018-05-28 12:49:53"
}
],
"validate":[
{
"created_at":"2018-05-28 12:49:53",
"deleted_at":null,
"id":2,
"key":"it_IT",
"name":"Italian (Italiano)",
"updated_at":"2018-05-28 12:49:53"
}
]
}
},
"error":false,
"message":null
}
]
Run Code Online (Sandbox Code Playgroud)
我想断言以下片段是响应的一部分:
1) ['translate' => [['key' => 'nl_NL']]]
2) ['validate' => [['key' => 'it_IT']]]
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以断言该translate数组至少包含一个键为的元素nl_NL并且validate包含一个键为的元素it_IT?
$response->assertSuccessful()->assertJsonFragment([
'translate' => [['key' => 'nl_NL']],
'validate' => [['key' => 'it_IT']
]);
Run Code Online (Sandbox Code Playgroud) 我真的不知道这里有什么问题.
我正在使用WAMP,这是我的路径
wamp
www
themeister
include
stream
gameslist.php
pages
test.php
Run Code Online (Sandbox Code Playgroud)
我在文件test.ph中并尝试用这个打开gameslist.php
<?php include('/themeister/include/stream/gameslist.php'); ?>
Run Code Online (Sandbox Code Playgroud)
它说该文件不存在.
我试图在我的HTML表单中触发控制器而不是路由.我似乎无法弄明白,大量的谷歌搜索只能回答Laravel 4的答案.
我的表单看起来像这样:
<form action="{{ ExpunctionIntakeController@getIndex }}" method="POST">
....
....
</form>Run Code Online (Sandbox Code Playgroud)
但这根本不起作用.我不想使用路由,因为我想返回视图而不是网址.
将控制器操作注入HTML的正确方法是什么?
php ×6
javascript ×2
python ×2
reactjs ×2
algolia ×1
autocomplete ×1
defined ×1
http-headers ×1
httpresponse ×1
json ×1
keyword ×1
laravel ×1
laravel-5 ×1
laravel-5.2 ×1
list ×1
mysql ×1
next.js ×1
numbers ×1
phpstorm ×1
properties ×1
symfony ×1
variance ×1
wamp ×1