测试“i”不是“x”或“y”(Python3)

时间:2017-09-16 08:02:25

标签: python python-3.x

我正在尝试检查“i”是否不等于“test”或“test2”。如果我将“i”设置为str(“test”)或str(“test2”),那么它将说“我不是test或test2”,如果我将“i”设置为str(“x”)之类的东西,功能正常。

    if str(i) != str("test") or str("test2"):
      print ("i is not test or test2")
    else:
      print ("i is test or test2")

2 个答案:

答案 0 :(得分:3)

or语句不起作用,or运算符需要boolean作为两个操作数,但在您的情况下,or应用于{ {1}}和boolnea运算符。在这种情况下,Python不会引发异常,并在内部将str转换为str值并处理True操作数。

现在您有两个选择,要么将if语句纠正为:

or

或者您可以使用:

if str(i) != str("test") or str(i) !=str("test2"):
    print ("i is neither test not test2")

答案 1 :(得分:0)

尝试:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="description" content="Demonstrates some logic errors" />
  <meta name="keywords" content="HTML, average, errors" />
  <title>Average numbers</title>
  <script src="badaverage.js"></script>
</head>
<body>
    <h1>Average numbers</h1>
    <p>Click the button to enter a series of numbers to average</p>
    <button id="enter">Enter your first number</button>
    <p><span id="numberList"></span></p>
    <button id="calculate">Calculate the average</button>
    <p><span id="result"></span></p>

</body>
</html>

/*
   JavaScript used with 'average.html'
   Use an interactive debugger to find the errors in the JavaScript below
*/

"use strict";
//Global variables accessible to all functions
var numbers = [];  //create an empty array
var enterButton = null;  //global variables must be initialised
var calculateButton = null;

/* Get a number from the prompt
*  If a valid number add to array, if not give error message to user
*  Display the updated array of numbers on the web page
*  Make the Calculate button visible
*/
function enterNumber(){
    var number = prompt("Enter your number");
    var number = Number(number);   //WHAT DOES THIS LINE DO?
    if (number >= 0) {    // test what is entered is a number   
        numbers.push(number);   //add the number entered to the end of the array 
    }
    else{
        alert("Please enter a valid number");
    }
    document.getElementById("numberList").innerHTML = "The numbers you have entered so far are: " + numbers;  //diplay a list of number entered
    enterButton.textContent = "Enter your next number"; //change the label on the Enter Button
    calculateButton.style.visibility = "visible";     //show the button  - uses the CSS property of the elenment
}       
/* Calculate the sum and average of the array of number
*  Display the results on the web page
*/
function calculateAverage(){
    var average = 0;
    var total = 0;
    for (var i = 0 ; i <= numbers.length; i++){    
        total = numbers[i];   //add the each number in the array to the cumulative total 
    }
    average = total/i;
    document.getElementById("result").innerHTML = "The total of your numbers is " + total + " and their average is " + average;
}

function init(){
        enterButton = document.getElementById("enter");  
        calculateButton = document.getElementById("calculate");
        calculateButton.style.visibility = "hidden";    //hide the Calculate button until some numbers are entered
        enterButton.onclick = enterNumber;
        calculateButton.onclick = calculateAverage;
}

window.onload = init;