Is it possible to use an if statement inside of an if statement?
For example:
If answer=10:
answer2=input("Do you agree")
if answer2=yes
print("You agree")
else:
print("You disagree")
else:
print("You don't answer")
For your question of an if statement in an if statement you would use a ternary operation:
if((condition1) ? condition2 : condition3)
code to execute
But I think you asked your question wrong. From your example, if answer = 10 you want to prompt the user for something and then do something different based on their response. It is hard to really tell you how to implement that without knowing what language you are writing this in but most languages have some way of creating a "code block", generally like this:
if(condition1)
{
if(condition2)
docode;
else
docode;
}
else
docode;
although some languages just us indentation
Related
Basically I made this code to ask a user if they want to play ball:
print("Do you want to play ball?")
answer = input("I will throw the ball and you will catch it! Yes or no?")
if answer == 'yes' or 'Yes': {
print("Great, lets play!")
}
elif answer == 'No' or 'no': {
print("owwkayyyy...")
}
However, when it runs, if I say no it still results with the program printing "great, lets play"
I am new to coding and I seriously do not understand why this isnt working.
It is because you need fix your condition for your first if statement. It is incorrect to write:
if answer == 'yes' or 'Yes':
You need to rewrite answer like so:
if answer == "yes" or answer == "Yes":
The same thing is true for your elif statement.
disclaimer: My title may not be accurate as far as what I would like to accomplish, but I can update if someone can correct my terminology
I have 2 functions, each with a separate purpose and usable on its own, but occasionally I would like to combine the two to perform both actions at once and return a single result, and to do this I would like to assign to a variable name
I know I can create a 3rd function that does basically what I want as it is really simple.. though it's become a bit of a challenge to myself to find a way of doing this
def str2bool(string):
return string.lower() in ("yes", "true", "t", "1")
def get_setting(string):
if string == 'cat':
return 'yes'
else:
return 'no'
VALID_BOOL = str2bool(get_setting)
print VALID_BOOL('cat')
So basically I would like to assign the combination of the 2 functions to a variable that I can call and pass in the string parameter to evaluate
In my real world code, get_setting() would retrieve a user setting and return the value, I would then like to test that value and return it as a boolean
Again I know I can just create a 3rd function that would get the value and do the quick test.. but this is more for learning to see if it can be done as I'm trying to do.. and so far my different variations of assigning and calling aren't working, is it even possible or would it turn too complex?
Using lambda is easy, but i don't know if it is exactly what you are looking for.
Example:
f = lambda astring : str2bool(get_setting(astring))
Outputs:
>>> f('cat')
True
Here is my hypothetical situation:
There is a list of people waiting outside of the club. The bouncer only allows people with ids 18 or older in the club. If they are younger than 18 they are denied admission. Suppose you know the ages of people in line that want to enter they are 18, 27, 16, 17. I created a code to try to run through it, but just cannot get it to work. It works for testing all the items 0 to 99, but I only want it to test items in the list. Can someone help me?
ages_of_people_waiting_outside_club = [18,27,16,17]
ages_of_people_waiting_outside_club = range(0,100)
for ids in range(0,100):
if ids >= 18:
print("you may enter")
else:
print("sorry, you can not come in")
In this instance, I would recommend using a foreach loop then nest in an if and else statement. The foreach loop will go through the whole list, then you can basically add in the if and else statements you showed above. Here would be an example using PHP:
foreach($age_of_people_waiting as $age) {
if($age >= 18){
print("you may enter");
}else{
print("sorry, you can not come in");
}
}
What this is doing is the array that contains the ages, in this case, $age_of_people_waiting, is being converted to an individual value for each section in the array, that value will be passed as $age. The if statement will then check if that particular $age value that it is on is 18 or over. If it is, it prints "you may enter" if not, it prints "sorry, you can not come in." I'm not sure what coding language you are using, but foreach loops are pretty common and exist in multiple languages, so it's your best bet.
That should work:
ages_of_people_waiting_outside_club = [18,27,16,17]
#ages_of_people_waiting_outside_club = range(0,100)
for ids in ages_of_people_waiting_outside_club:
if ids >= 18:
print("you may enter")
else:
print("sorry, you can not come in")
I do not know why there is a syntax error, this is so simple but keeps coming up wrong.
number = float(input("How much shall you be spending today?")
if number>10
print("You get 20% off")
else
print("You get 10% off ")
Your if/else statement is indented, and shouldn't be. Additionally, you need : after the if/else statements, and a second ) on the first line. Finally, you should be using raw_input() with Python 2.7. Your code block should look like this:
number = float(raw_input("How much shall you be spending today? "))
if number > 10:
print("You get 20% off")
else:
print("You get 10% off")
In your first line, you said 'input' but the correct function was raw_input. Also, the indentation for your if statement is wrong which can cause problems.
Why does the following code always emit 'yes' (in HTML) even when sel has a different value other than 'audio'? (I've logged the values of sel and confirmed different values.)
This is my day 1 on Play. So, I may be doing something really stupid.
${sel='audio'?'yes':'no'}
I don't know the Groovy templates that well, but I think it should be like this:
${sel=='audio'?'yes':'no'}
Your code:
${sel='audio'?'yes':'no'}
means; if 'audio' (a string that is not null will always be "true") then sel = 'yes' otherwise 'no', so it will never be 'no'.
You might want to look at the yesNo extension: http://www.playframework.org/documentation/1.2.1/javaextensions#yesNo