Why don't you put a variable before an else statment?

  • Thread starter Cash Fulton
  • Start date
  • Tags
    Variable
In summary, the else keyword in Python applies to a section of code and not to variables. Its purpose is to provide an alternative action when the boolean expression in the if statement is false. This allows for more flexibility in handling different cases and conditions.
  • #1
Cash Fulton
24
1
I'm too lazy to see the big picture. Here is an example to show you what I'm talking about:

x = str(input())
if x == "that's a nice meme sir":
print("thanks friend")
elif x == "crap meme stop posting anytime":
print("oh")

else:
print("okay")


Why don't you put the variable in front of else? Don't you have to clarify what variable you want else to apply to? Thanks.
 
Technology news on Phys.org
  • #2
You can think of the else in a couple of ways. One, as being there in error... If you expect a variable to be either one or two with different actions for each value you can do an if and an elif for one and two then if you end up in the else, it is because the value is not one or two... So you may want to place an error message in the else and even possibly quit the program

The other possibility, it is simply the you don't care what the value is our you have done enough validation before hand that you know what the value will be... For example, if you have an if x<1 followed by an else, then clearly x will be >= 1 in the else.
 
  • #3
Cash Fulton said:
Why don't you put the variable in front of else? Don't you have to clarify what variable you want else to apply to? Thanks.
The else keyword doesn't apply to variables. It applies to a section of code in your Python code.

The output from the sample below is
x is not 1
Done

Python:
import sys
x = 2
if x == 1:
  print('x is 1')
else:
  print('x is not 1')
print('Done')
If the expression x == 1 is true, the first print statement executes, and then the code displays Done.
If the expression x == 1 is false, the second print statement executes, after which the code again displays Done.

The value of the variable x is immaterial; what's important is whether the boolean expression happens to be true (in which case the statement or statements associated with the if portion execute) or false (causing the statements associated with the else clause to execute).
 

1. Why is it necessary to have a variable before an else statement?

Having a variable before an else statement allows for more control and flexibility in the program. It allows the program to check for a specific condition and execute a different set of instructions if the condition is not met.

2. Can't I just use an if statement without a variable before the else statement?

Yes, you can use an if statement without a variable before the else statement, but it may not be as efficient. Using a variable allows for a more concise and organized code, and also enables the program to handle multiple conditions and outcomes.

3. What happens if I don't use a variable before an else statement?

If you don't use a variable before an else statement, the program may still run, but it may not have the desired outcome. Without a variable, the program may not be able to differentiate between different conditions and execute the appropriate set of instructions.

4. Is there a specific reason why a variable is typically used before an else statement?

Yes, a variable is typically used before an else statement for better code organization and readability. It also allows for more complex conditions and outcomes to be handled in a more efficient manner.

5. Can I use any type of variable before an else statement?

Yes, you can use any type of variable before an else statement as long as it can hold a value that can be evaluated by the program. This can include integers, booleans, strings, and more.

Similar threads

  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
13
Views
19K
  • Atomic and Condensed Matter
Replies
5
Views
3K
  • Special and General Relativity
Replies
29
Views
1K
  • General Math
Replies
4
Views
2K
Replies
2
Views
2K
Back
Top