在我的代码中,我创建了操作函数,因为它们将在整个代码中多次使用。但是,当我尝试在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语句时,它们会在调用时按预期工作。我确定我在某个地方犯了一个愚蠢的错误,但我似乎无法找到它。 任何帮助是极大的赞赏。谢谢。
答案 0 :(得分:1)
问题是输入返回一个字符串并将其与int类型变量进行比较,因此永远不会调用您的函数:
if ask == '1' :
print(sendSMS())
print("SMS sent.")
if ask == '2' :
print(sendEmail())