Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/Else Statement Help!
#1
Hello
I am teaching myself python so sorry for the silly question. I wrote something very simple. If Joe calls, say "Hi Joe." If someone else calls say "How may I help?". Every time I will get back Hi, Joe regardless of the input. Can someone tell me what I am doing wrong? I am using Azure Notebooks:

friend = "Joe"
call = input("Who is this?")
if friend:
    print("Hi, Joe.")

else:
    print("How may I help?")

Ok dumb question. I forgot to insert

if call == friend
Reply
#2
if friend Checks whether "friend" is True or not. In if statements, a non-empty string evaluets to True (conversely, empty string evaluates to False). That is why your if condition is always True.

Instead you probably want:
if call == friend
Reply


Forum Jump:

User Panel Messages

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