Python Forum
I want to create an if-condition, but I'm not sure how to.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to create an if-condition, but I'm not sure how to.
#1
I want to create an if-condition, but I'm not sure how to do it... I would like, that if the else-condition is true, it prints my message. How can I do that?

That's my recent try:

y =  'lol'
if y == True:
    print ('y ist wahr!')
elif y == False:
    print ('y ist nicht wahr!')
else:
    print ('y ist weder wahr noch nicht wahr')

if else == True:
    print ('Ich habe es, denke ich, verstanden')
And that's the error I get:

Error:
if else == True: ^ SyntaxError: invalid syntax
I'm a starter, so it's probably easy to solve for you professionals Tongue
Reply
#2
something = False
else_true = False

if something:
    print('something is True')
else:
    print('something is False')
    else_true = True

if else_true:
    print('else_true is True')
Reply
#3
It doesn't make sense for else to have a condition. Like in English, it means "in the other case". That's also why you can't have an else on its own.
Reply
#4
(Aug-09-2020, 10:55 AM)ndc85430 Wrote: It doesn't make sense for else to have a condition. Like in English, it means "in the other case". That's also why you can't have an else on its own.

What do you mean with "you can't have an else on its own"? Could you explain that please?
Reply
#5
As in, you can't write

else:
  # something here
because again, else means "in the other case", so it has to follow an if or elif.

Of course, an if on its own makes sense. You might want to do something when the condition is True, but nothing if it is False.
Reply
#6
(Aug-09-2020, 04:15 PM)ndc85430 Wrote: As in, you can't write

else:
  # something here
because again, else means "in the other case", so it has to follow an if or elif.

Of course, an if on its own makes sense. You might want to do something when the condition is True, but nothing if it is False.

Alright, got it Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create new column based on condition arvin 12 2,126 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  else condition not called when if condition is false Sandz1286 10 5,736 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,698 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Substring and If then Condition to create column Chandan 2 2,319 Jan-23-2020, 08:40 AM
Last Post: buran
  Create new list from another list based on condition khalidreemy 1 2,347 Jun-11-2019, 10:21 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020