将二次根放入列表中

时间:2016-04-24 17:54:58

标签: python-3.x

def quadratic_roots(a,b,c):
        if not (type(a)==int or type(a)==float) and (type(b)==int or type(b)==float)\
   and (type(c)==int or type(c)==float):
    print("Error. Must be numbers.")
    return None
equation=(b**2)-(4*a*c)
realRoots=[]
if equation<0:
    return realRoots
elif equation==0:
    x1=-b/(2*a)
    #realRoots.append(x)
    return [x1]
else:
    x1=(-b+((equation)^(1/2))/(2*a)
    x2=(-b-((equation)^(1/2))/(2*a)
    return realRoots.append(x1,x2)

我需要将我的二次根写入一个列表,但我不断收到语法错误。如何编辑我的代码以使其正常运行?

2 个答案:

答案 0 :(得分:1)

append只接受一个参数并返回Nonedocumentation):

return realRoots.append(x1,x2)  # cannot work

# Instead, either `append` one by one:
realRoots.append(x1)
realRoots.append(x2)
return realRoots

# or use 'extend':
realRoots.extend([x1, x2])
return realRoots

# or the simplest
return [x1, x2]

顺便说一下,Python中的'power'-operator是**,而不是^

> 2**3
8

> 2^3
1

答案 1 :(得分:0)

正如@schwobaseggl所说,你必须改变幂运算符和append语法。 另请注意,append方法不返回任何值(默认为None) 因此 <?php error_reporting(E_ALL); ini_set('display_errors', 1); //--------------------------------------DELETE RECORD - DATABASE----------------------------------------------- //Obtain login credentials require_once 'login.php'; //Create connection to database $conn = new mysqli($hn, $un, $pw, $db); //Check if connection succeeded if ($conn->connect_error) die($conn->connect_error); //Verify if user already chose record to be deleted from the database table if (isset($_GET['id']) && is_numeric($_GET['id'])) { $id = $_GET['id']; //Delete record from table $query = "DELETE FROM jazz WHERE id=$id"; $result = $conn->query($query); //If query fails, display error message if (!$result) { echo "DELETE failed: $query <br />" . $conn->error . "<br /><br />"; } else { //If query succeeded, display success message echo "Record deleted successfully!"; } } //--------------------------------------RETRIEVE RECORDS - DATABASE----------------------------------------------- //Retrieve records $query = "SELECT id, year, artist, album, Publisher, subgenre, rating FROM jazz"; $result = $conn->query($query); //Check if query succeeded if (!$result) die ("Database access failed: " . $conn->error); //Sets up Multi Delete Ability **if(isset($_POST['delete'])) { $cnt=array(); $cnt=count($_POST['checkbox']); for($i=0;$i<$cnt;$i++) { $del_id=$_POST['checkbox'][$i]; $query = "DELETE FROM jazz WHERE id='$.id'"; $result = $conn->query($query); if (!$result) echo "DELETE failed: $query<br>" . $conn->error . "<br><br>"; # mysqli_query($conn , $result); } }** echo "<!DOCTYPE html>"; echo "<html>"; echo "<head>"; echo "<title>Delete Record</title>"; echo "<link rel='stylesheet' href='view.css'>"; echo "</head>"; echo "<body>"; echo "<header><h1>Delete Record</h1></header>"; echo "<form action = 'delete_test.php' method = 'post'>"; echo "<table>"; echo "<tr class='row'>"; echo "<th class='col-1'>ID</th>"; echo "<th class='col-1'>Year</th>"; echo "<th class='col-2'>Artist</th>"; echo "<th class='col-3'>Album</th>"; echo "<th class='col-4'>Rating</th>"; echo "<th class='col-5'>Subgenre</th>"; echo "<th class='col-6'>Publisher</th>"; echo "<th class='col-7'>Delete</th>"; echo "</tr>"; **//Display retrieved records ----------------START if (is_object($result)){ $rows = $result->num_rows; } $rows = $result->num_rows;** ----------------END for ($j = 0 ; $j < $rows ; ++$j) { $result->data_seek($j); $row = $result->fetch_array(MYSQLI_NUM); echo "<tr class='row'>"; echo "<td class='col-1'>" . $row[0] . "</td>"; echo "<td class='col-2'>" . $row[1] . "</td>"; echo "<td class='col-3'>" . $row[2] . "</td>"; echo "<td class='col-4'>" . $row[3] . "</td>"; echo "<td class='col-5'>" . $row[4] . "</td>"; echo "<td class='col-6'>" . $row[5] . "</td>"; echo "<td class='col-7'>" . $row[6] . "</td>"; echo "<td class='col-8'><input type = 'checkbox' input name = 'checkbox[]' value='.$row[0].'</td>"; echo "</tr>"; } echo "<input type = 'submit' value = 'delete' name = 'delete' class = 'button'>"; echo "</table></form></body></html>"; echo "<a href='Project - Homepage.html'>Home</a>"; //Close connection $conn->close(); ?> return realRoots.append(x1)添加到x1,但会返回realRoots