【问题标题】:Python menu integration errorPython菜单集成错误
【发布时间】:2015-12-25 10:17:25
【问题描述】:

所以,我一直在尝试用 Python 制作一个 Skype 多功能工具,我刚刚开始。我有一个可以运行的程序的代码,但我不知道如何将它放入菜单中。

这是我得到的代码:

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 是一个无效的语法。

我该如何解决这个问题,我做错了什么?

【问题讨论】:

  • 请修正缩进。为什么要在 if 语句中定义函数?
  • 在哪里?抱歉,我刚开始使用 python...

标签: python syntax menu


【解决方案1】:

这是一个正确缩进的代码。但是,不要忘记,在 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")

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 2019-08-06
    • 2013-05-20
    • 1970-01-01
    • 2019-01-25
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多