我有一个.py脚本,可以在TextWrangler中完美运行,但在我尝试导入Terminal或IDLE时无效。我不断收到此错误消息:
追踪(最近一次呼叫最后一次):
文件“stdin”,第1行,在“模块”中 文件“c.py”,第1行 bookic08ic14x.is32
我不知道这意味着什么。在进行导入之前,我总是确保我在正确的目录中。
我的代码如下所示:
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)
选择()
如果有人建议让这段代码更好,我愿意接受!
答案 0 :(得分:0)
在定义函数之前,您应首先导入random
模块。
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())
这样做会更好,如果您在功能中不使用return
,则无法在任何地方使用您的功能。例如,通过这种方式,您可以使用.format
方法使用函数。
这个运行完美,我查了一下。