views:

27

answers:

2

Hi there! 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 iputip.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!

+1  A: 

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()
Ivo van der Wijk
Omg!!! Great answer! looks like input needed a real value in order to be executed. Thanks mate!
JavaNoob
input() uses eval() on the entered data. This is usually not what you want (and also rather unsafe, it accepts python expressions). As I explained, use raw_input and parse yourself.
Ivo van der Wijk
A: 

I don't think execfile() is good practice. Try use 'import' command instead:

if choice == 1:
    from Inputip import some_func
    some_func()

Inputip should then contain some method you can import, of course.

yedpodtrzitko