函数将int输入与另一个int进行比较

时间:2018-02-01 23:42:39

标签: python python-3.x

我今天开始学习Python,并且我开始学习各种功能。我想比较输入,如果低于等于或高于例如5.它不能正常工作或我最终得到无效的语法

(
 [t1] => Array
    (
        [s1-1] => 1
        [s1-2] => 1
        [s2-1] => 1
        [s2-2] => 1
        [s3-1] => 0
        [s3-2] => 0
    )

[t2] => Array
    (
        [s1-1] => 1
        [s2-1] => 2
        [s2-2] => 1
        [s1-2] => 0
        [s3-1] => 0
        [s3-2] => 0
    )

[t3] => Array
    (
        [s1-1] => 1
        [s2-1] => 1
        [s3-1] => 1
        [s3-2] => 3
        [s1-2] => 0
        [s2-2] => 0
    )

)

输出:

<!DOCTYPE html>
<?php
// arrays of arrays, I don't know how you receive the data
$arrays = [
    ['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1'],
    ['t2-s1-1=1', 't2-s2-1=2', 't2-s2-2=1'],
    ['t3-s1-1=1', 't3-s2-1=1', 't3-s3-1=1', 't3-s3-2=3']
];

// bi-dimensional array
$output = [];

// it will store all columns you find in the $arrays entry
$allColumns = [];

// iterate for every array you receive, i.e. ['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1']
foreach ($arrays as $array) {
    // iterate over every element in the array: 't1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1' and 't1-s2-2=1'
    foreach ($array as $item) {
        // extract the parts on every element: $matches is an array containing the different parts
        preg_match('/^(t\d+)-(s\d+-\d+)=(\d+)/', $item, $matches);
        /**
         * $matches[0] would contains the element if matched: 't1-s1-1=1'
         * $matches[1] would contains 't1' if matched
         * $matches[2] would contains 's1-1' if matched
         * $matches[2] would contains 1 (integer) if matched
         */
        if (!empty($matches)) {
            $output[$matches[1]][$matches[2]] = $matches[3];
            $allColumns[] = $matches[2];
        }
    }
}

// clean duplicates
$allColumns = array_unique($allColumns);

// sort values alphabetically
sort($allColumns);

// iterate over the just created bi-dimensional array
foreach ($output as $row => $columns) {
    // iterate for all columns collected before
    foreach ($allColumns as $column) {
        // if one of column in 'allColumns' doesn't exit in $output you added in the correct place adding a zero value
        if (!in_array($column, array_keys($columns))) {
            $output[$row][$column] = 0;
        }
    }
}
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Table Page</title>
</head>
<body>

<table>
    <thead>
    <?php
        echo '<tr><th>Test</th>';
        foreach ($allColumns as $head) {
            echo sprintf('<th>%s</th>', $head);
        }
    echo '</tr>';
    ?>
    </thead>
    <tbody>
    <?php
        foreach ($output as $key => $columns) {
            echo sprintf('<tr><td>%s</td>', $key);
            foreach ($columns as $column) {
                echo sprintf('<td>%s</td>', $column);
            }
            echo '</tr>';
        }
    ?>

    </tbody>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

虽然在许多情况下使用set()是个好主意,但它可以在列表中使用。

在上面给出的示例中,您似乎并未实际使用列表,因此您不需要set()。在没有它的情况下重写它:

def functionX():
    fce = int(input('Enter number: '))
    if fce<=5:
        print('equal or lower than 5')
    else:
        print('higher than 5')
functionX()