if-statement

How do I test if a variable does not equal either of two values?

泪湿孤枕 提交于 2019-12-27 19:14:06
问题 I want to write an if/else statement that tests if the value of a text input does NOT equal either one of two different values. Like this (excuse my pseudo-English code): var test = $("#test").val(); if (test does not equal A or B){ do stuff; } else { do other stuff; } How do I write the condition for the if statement on line 2? 回答1: Think of ! (negation operator) as "not", || (boolean-or operator) as "or" and && (boolean-and operator) as "and". See Operators and Operator Precedence. Thus: if

Check whether $_POST-value is empty

不问归期 提交于 2019-12-27 14:44:24
问题 if(isset($_POST['submit'])) { if(!isset($_POST['userName'])) { $username = 'Anonymous'; } else $username = $_POST['userName']; } I cannot get the $username to be "Anonymous"? It is either blank or the value of $_POST['userName'] . 回答1: isset() will return true if the variable has been initialised. If you have a form field with its name value set to userName , when that form is submitted the value will always be "set", although there may not be any data in it. Instead, trim() the string and

Check whether $_POST-value is empty

风格不统一 提交于 2019-12-27 14:42:38
问题 if(isset($_POST['submit'])) { if(!isset($_POST['userName'])) { $username = 'Anonymous'; } else $username = $_POST['userName']; } I cannot get the $username to be "Anonymous"? It is either blank or the value of $_POST['userName'] . 回答1: isset() will return true if the variable has been initialised. If you have a form field with its name value set to userName , when that form is submitted the value will always be "set", although there may not be any data in it. Instead, trim() the string and

What is a None value?

ⅰ亾dé卋堺 提交于 2019-12-27 10:43:43
问题 I have been studying Python, and I read a chapter which describes the None value, but unfortunately this book isn't very clear at some points. I thought that I would find the answer to my question, if I share it there. I want to know what the None value is and what do you use it for? And also, I don't get this part of the book: Assigning a value of None to a variable is one way to reset it to its original, empty state. What does that mean? The answers were great, although I didn't understand

Test for non-zero length string in Bash: [ -n “$var” ] or [ “$var” ]

心不动则不痛 提交于 2019-12-27 10:12:48
问题 I've seen Bash scripts test for a non-zero length string in two different ways. Most scripts use the -n option: #!/bin/bash # With the -n option if [ -n "$var" ]; then # Do something when var is non-zero length fi But the -n option isn't really needed: # Without the -n option if [ "$var" ]; then # Do something when var is non-zero length fi Which is the better way? Similarly, which is the better way for testing for zero-length: if [ -z "$var" ]; then # Do something when var is zero-length fi

Test for non-zero length string in Bash: [ -n “$var” ] or [ “$var” ]

早过忘川 提交于 2019-12-27 10:12:10
问题 I've seen Bash scripts test for a non-zero length string in two different ways. Most scripts use the -n option: #!/bin/bash # With the -n option if [ -n "$var" ]; then # Do something when var is non-zero length fi But the -n option isn't really needed: # Without the -n option if [ "$var" ]; then # Do something when var is non-zero length fi Which is the better way? Similarly, which is the better way for testing for zero-length: if [ -z "$var" ]; then # Do something when var is zero-length fi

Better to 'try' something and catch the exception or test if it's possible first to avoid an exception?

三世轮回 提交于 2019-12-27 09:54:12
问题 Should I test if something is valid or just try to do it and catch the exception? Is there any solid documentation saying that one way is preferred? Is one way more pythonic ? For example, should I: if len(my_list) >= 4: x = my_list[3] else: x = 'NO_ABC' Or: try: x = my_list[3] except IndexError: x = 'NO_ABC' Some thoughts... PEP 20 says: Errors should never pass silently. Unless explicitly silenced. Should using a try instead of an if be interpreted as an error passing silently? And if so,

Better to 'try' something and catch the exception or test if it's possible first to avoid an exception?

夙愿已清 提交于 2019-12-27 09:53:32
问题 Should I test if something is valid or just try to do it and catch the exception? Is there any solid documentation saying that one way is preferred? Is one way more pythonic ? For example, should I: if len(my_list) >= 4: x = my_list[3] else: x = 'NO_ABC' Or: try: x = my_list[3] except IndexError: x = 'NO_ABC' Some thoughts... PEP 20 says: Errors should never pass silently. Unless explicitly silenced. Should using a try instead of an if be interpreted as an error passing silently? And if so,

How to solve the else/if problems in password input?

狂风中的少年 提交于 2019-12-26 21:39:26
问题 I wrote a program that users can login. Below u see some codes that I wrote for the password input but the second if of them does not work properly. Please help me to find the problem. Why it does not work? import java.util.Scanner; public class Password { public static void main(String[] args) { Scanner passwordInput = new Scanner(System.in); System.out.print("Please enter your password: "); int builtInPassword = 1254; if (passwordInput.hasNextInt() && passwordInput.nextInt() ==

Python If statement and logical operator issue [duplicate]

China☆狼群 提交于 2019-12-26 18:39:51
问题 This question already has answers here : How to test multiple variables against a value? (24 answers) Closed 2 years ago . I'm trying to create a small python program to emulate rolling a single die. My problem arises in that I want the program to accept both upper and lower-case 'Y' to trigger a re-roll. My program works when I accept either upper or lower case y but when I try to change the line if usr == 'Y' to if usr == 'Y' or 'y' creates a problem where it never enters the else statement