在使用 python 检测输入数据的数据类型时,如何使用嵌套的 if-else 来完成这项工作

问题描述

我想知道在使用if else语句并检测输入的数据是什么类型的数据时,如何使python程序工作。

这是我根据我的知识输入的代码(抱歉我是新手):

<!DOCTYPE html>

<html>

<head>
    <Meta charset="utf-8">
    <title>Title</title>

<style>
#area1 {width:100px;height:100px;}
#area2 {width:100px;height:100px;}
#area3 {width:100px;height:100px;}
#area4 {width:100px;height:100px;}
.black {background-color:#000;}
.white {background-color:#fff;}
</style>
</head>

<body>
    <div id="area1" class="black" onclick="change1();"></div>
<br>
    <div id="area2" class="black" onclick="change2(this);"></div>
<br>
    <div id="area3" class="black"></div>
<br>
    <div id="area4" class="black"></div>

<script>
area1 = document.querySelector('#area1');

function change1(){
    area1.classList.replace('black','white');
    console.log('change1');
}

function change2(item){
    item.classList.replace('black','white');
    console.log('change2');
}

function change3(){
    this.classList.replace('black','white');
    console.log('change3');
}

area3 = document.querySelector('#area3');
area3.addEventListener('click',change3);

area4 = document.querySelector('#area4');
area4.addEventListener('click',function(){
    this.classList.replace('black','white');
    console.log('change4');
});

</script>

</body>

</html>

如果您能纠正我的工作,请随意,我将不胜感激。谢谢!

解决方法

  1. 你可能是说float or int不是float and int
  2. 你不能使用和/或那样你需要说 type(eval(data)) == float or type(eval(data)) == int 否则你会得到奇怪的行为,因为 or 运算符将 something == something (这是一个布尔值)与不是布尔值的其他类型进行比较。
  3. 如果你使用 elif,你真的不需要嵌套的 if 语句 ( if int ... elif float ... else )