0

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.

asked Oct 31, 2017 at 2:10

1 Answer 1

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())
answered Oct 31, 2017 at 2:14
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this fixed everything. I was sure I remember trying this earlier, but it might have been in a different scenario, so I didn't think to try it again. Anyway, thanks again for the help and quick answer. Have a nice day. :)
@deadturkey no problem !

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.