2015-12-25 46 views
0

所以,我一直在嘗試在Python中創建一個Skype多工具,我剛開始。我有一個可以工作的程序的代碼,但我無法弄清楚如何將它放入菜單中。Python菜單集成錯誤

下面是代碼我得到:

import time 
import urllib2 
ans=True 
while ans: 
print(""" 
1. Skype resolver 
2. Option 2 
3. Option 3 
4.Exit/Quit 
""") 
ans=raw_input("What would you like to do? ") 
if ans=="1":(
def getSkype(): 
input = '> ' 
print "Please enter a skype name: " 
skypename = raw_input(input) 
print "Skype name: %s" % skypename 
time.sleep(2.5) 
url = "http://APIOnly.com/skype.php?username=%s" % skypename 
req = urllib2.Request(url) 
response = urllib2.urlopen(req) 
return response.read() 

if __name__ == "__main__": 
skypeip = getSkype() 
print "IP: %s" % skypeip 
print "Press [Enter] to continue" 
raw_input() 
) 
    time.sleep(20) 
elif ans=="2": 
    print("Option 2") 
elif ans=="3": 
    print("\n Option 3") 
elif ans=="4": 
    print("\n Goodbye") 
    ans = None 
else: 
     print("\n Not Valid Choice Try again") 

它告訴我,def是無效的語法。

我該如何解決這個問題,以及我做錯了什麼?

+1

請修復您的縮進。你爲什麼要在if語句中定義一個函數? –

+0

哪裏?對不起,但我剛開始python ... – Pete

回答

0

這是一個正確縮進的代碼。但是,不要忘記,在Python中,代碼的縮進具有語義含義,因此您無法獲得「非縮進」代碼。

import time 
import urllib2 

ans=True 

while ans: 
    print(""" 
     1. Skype resolver 
     2. Option 2 
     3. Option 3 
     4.Exit/Quit 
    """) 
    ans=raw_input("What would you like to do? ") 
    if ans=="1":(
     def getSkype(): 
      input = '> ' 
      print("Please enter a skype name: ") 
      skypename = raw_input(input) 
      print("Skype name: %s" % skypename) 
      time.sleep(2.5) 
      url = "http://APIOnly.com/skype.php?username=%s" % skypename 
      req = urllib2.Request(url) 
      response = urllib2.urlopen(req) 
      return response.read() 

     if __name__ == "__main__": 
      skypeip = getSkype() 
      print("IP: %s" % skypeip) 
      print("Press [Enter] to continue") 
      raw_input() 
     ) 
     time.sleep(20) 
    elif ans=="2": 
     print("Option 2") 
    elif ans=="3": 
     print("\n Option 3") 
    elif ans=="4": 
     print("\n Goodbye") 
     ans = None 
    else: 
     print("\n Not Valid Choice Try again")