in my code, I have created functions for operations as they will be used multiple times throughout the code. However, when I try to call the functions within an if statement the programme just exits. This is the part of the code in question:
def sendEmail():
emailReceiver = input("Who will receive the email?\n")
emailServer.login("xxxxxxx", "xxxxxx") #logs in
with provided email and password
emergency = input("What is your emergency?\nTry to include a description of what has happened, names of people involved, and the location.\n")
msg = MIMEMultipart('alternative') #sets up the email so it has correct formatting
msg['Subject'] = "Emergency Alert"
msg['From'] = "Emergency Alert"
msg['To'] = emailReceiver
textBody = emergency
part1 = MIMEText(textBody, 'plain') #makes sure the email is in text format
rather than HTML
msg.attach(part1)
emailServer.sendmail("xxxxxxx", emailReceiver, msg.as_string())
print("Alert sent.")
def sendSMS():
message = input("what would you like to send? ".as_string())
client.api.account.messages.create(
to = "xxxxxxxx",
from_ = "xxxxxxxx",
body = message)
def makeCall():
makeCall = client.api.account.calls.create(
to = "xxxxxxxx",
from_ = "xxxxxxxx",
url = "xxxxxxxx")
ask = input(" Choose option:\n 1. Send SMS\n 2. Send email\n 3.Make phone call\n 4. Send SMS and email\n 5. Send SMS and make call\n 6. Send Email and make call")
if ask == 1 :
print(sendSMS())
print("SMS sent.")
if ask == 2 :
print(sendEmail())
Even though the functions don't actually function when called in the if statement, they work as intended when called when they're not part of the if statement. I'm sure I've just made a stupid mistake somewhere but I can't seem to find it. Any help is greatly appreciated. Thank you.
1 Answer 1
The problem is that input returns a string and you compare it to a int type variable, therefore your function is never called:
if ask == '1' :
print(sendSMS())
print("SMS sent.")
if ask == '2' :
print(sendEmail())