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

  • Thread starter Thread starter Cash Fulton
  • Start date Start date
  • Tags Tags
    Variable
AI Thread Summary
The discussion centers around the use of the "else" statement in Python programming. It clarifies that "else" does not apply to specific variables but rather to a block of code that executes when the preceding conditions are not met. The conversation highlights two main interpretations of "else": one as a catch-all for unexpected values, suggesting the inclusion of error messages, and the other as a continuation of logic when prior conditions have already validated the variable's state. An example is provided to illustrate that the execution of code depends on the truth value of a boolean expression rather than the variable itself. The output of the example reinforces that the focus is on the condition's result, not the variable's value.
Cash Fulton
Messages
24
Reaction score
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
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.
 
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).
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top