2017-10-31 10 views
0

私のコードでは、コード全体で何度も使用されるため、操作のための関数を作成しました。しかし、if文の中で関数を呼び出そうとすると、プログラムはただ終了します。 これは、問題のコードの一部です:私の関数が正しく呼び出されないのはなぜですか?

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()) 

if文で呼び出されたときの機能は、実際に機能していないにもかかわらず、意図したように、彼らはif文の一部じゃない時に呼び出されたとき、彼らは仕事。私はちょうどどこかで愚かな間違いをしたと確信していますが、私はそれを見つけるように見えません。 何か助けていただければ幸いです。ありがとうございました。したがって、あなたの関数が呼び出されることはありません、

答えて

1

問題は、入力文字列を返すことで、あなたはint型の変数にそれを比較:

if ask == '1' : 
    print(sendSMS()) 
    print("SMS sent.") 

if ask == '2' : 
    print(sendEmail()) 
+0

は、この固定のすべてをありがとうございます。私は以前これを試したことを覚えていたと確信していましたが、それは別のシナリオにあったかもしれないので、もう一度試してみることは考えませんでした。とにかく、助けと素早い答えにもう一度感謝します。良い一日を。 :) – deadturkey

+0

@deadturkey問題ありません! – scharette

関連する問題