1

I have a .py script that runs perfectly in TextWrangler but will not work when I try to imported into Terminal or IDLE. I keep getting this error message:

Traceback (most recent call last):

File "stdin", line 1, in "module"
File "c.py", line 1
bookic08ic14x.is32

I have no idea what this means. I always make sure that I am in the correct directory before I make the import.

My code looks like the following:

def choice():
import random
print "This program decides who has to do the job"
n = input("How many people are there?:")
people = []
for i in range (1, n+1):
    subject = raw_input("Person %s:" % i)
    people.append(subject)
print random.choice(people)

choice()

Also if anyone has suggestions for making this code better I am open to that!

noble-k
  • 13
  • 1
  • 2

1 Answers1

0

You should import the random module first, before defining the function.

import random
def choice():
    print ("This program decides who has to do the job")
    n = input("How many people are there?:")
    people = []
    while len(people)<int(n):
        subject=input("Enter the names: ")
        people.append(subject)
    x=random.choice(people)
    return x
print (choice())

This is better, if you don't use return in your function then you can't use your function wherever you want. For example, with this way you can use your function with .format method. This one running perfectly, I checked.

Daniel Darabos
  • 26,991
  • 10
  • 102
  • 114
GLHF
  • 3,835
  • 10
  • 38
  • 83
  • and please check it as correct answer if it solved your issue. – GLHF Dec 05 '14 at 06:40
  • Works great thank you! I just changed input to raw_input in the while loop so that I wouldn't have make each name into a string in IDLE. – noble-k Dec 05 '14 at 13:10
  • You must be using 2.x. 3.x input == 2.x raw_input. There are rare occasions where putting an import inside a function is appropriate, but doing so is normally a pure waste. PEP 8, and I, recommend spaces around '=' in assignment statements (but no spaces for arg=value in a function call). Makes code easier to read. – Terry Jan Reedy Dec 05 '14 at 23:06