“语法错误:解析时出现意外的 EOF”当使用输入()时

发布于 2024-09-26 00:36:47 字数 1342 浏览 4 评论 0原文

我有 2 个 Python 脚本,分别是 main_menu.py 和 inputip.py。

当我的函数在 inputip.py 中完成时,当我按“enter”重定向到 main_menu.py 时,就会出现问题。该脚本不允许我重定向到 main_menu.py,而是在 Windows 命令提示符上显示此错误:

Traceback (most recent call last):
  File "C:\python\main_menu.py", line 32, in ?
    execfile('C:\python\Inputip.py')
  File "C:\python\Inputip.py", line 11, in ?
    input ("\nSelect enter to proceed back to Main Menu\n")
  File "<string>", line 0

^ SyntaxError: 解析时出现意外的 EOF

以下是我的代码 (main_menu.py):

def menu():
#print what options you have
print "Welcome to Simple Network Program"
print " "
print "Please enter a following option to proceed"
print " "
print "2) View Personal IP Address"
print " "
return input ("Select an Option here: ")
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
    execfile('Inputip.py')
elif choice == 5:
    loop = 0
print "Thank you for using the Simple Network Program!"

代码 (inputip.py):

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.
import socket
import os
print ("\n\n"+socket.gethostbyname(socket.gethostname()))
input ("\nSelect enter to proceed back to Main Menu\n")

execfile('C:\python\main_menu.py')

错误似乎指向 execfile。关于代码的一些建议会很棒。谢谢!

I have 2 Python scripts which are main_menu.py and inputip.py.

The problem occurs when I press "enter" to be redirected to main_menu.py when my function finishes in inputip.py. The script does not allow me to redirect to main_menu.py instead it shows this error on the Windows command prompt:

Traceback (most recent call last):
  File "C:\python\main_menu.py", line 32, in ?
    execfile('C:\python\Inputip.py')
  File "C:\python\Inputip.py", line 11, in ?
    input ("\nSelect enter to proceed back to Main Menu\n")
  File "<string>", line 0

^
SyntaxError: unexpected EOF while parsing

Here are my codes (main_menu.py):

def menu():
#print what options you have
print "Welcome to Simple Network Program"
print " "
print "Please enter a following option to proceed"
print " "
print "2) View Personal IP Address"
print " "
return input ("Select an Option here: ")
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
    execfile('Inputip.py')
elif choice == 5:
    loop = 0
print "Thank you for using the Simple Network Program!"

The code (inputip.py):

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.
import socket
import os
print ("\n\n"+socket.gethostbyname(socket.gethostname()))
input ("\nSelect enter to proceed back to Main Menu\n")

execfile('C:\python\main_menu.py')

The error seems to be pointing to the execfile. Some advice on the codes would be great. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-10-03 00:36:47

除非您使用 python 3.x(但您的问题没有这样标记),否则不要使用输入。使用 raw_input 代替。它将返回字符串,因此首先将它们转换为 int,或者进行字符串比较。例如

x = raw_input("Choice")
if x == '1': 
    do_this()

Unless you are using python 3.x (but your question is not tagged as such), don't use input. Use raw_input in stead. It will return strings, so convert them to int first, or do a string comparison. E.g.

x = raw_input("Choice")
if x == '1': 
    do_this()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文