随机函数未在Python中正确导入

时间:2015-02-22 22:44:54

标签: python-2.7 importerror

我做了一个简单的石头剪刀游戏,工作得很好,但现在它出现了错误:

Traceback (most recent call last):
  File "RPS.py", line 1, in <module>
    from random import random
  File "/Users/gunnarvonhaden/Desktop/random.py", line 47, in <module>
    print rock()
  File "/Users/gunnarvonhaden/Desktop/random.py", line 6, in rock
    a = random()
NameError: global name 'random' is not defined
logout

这与我之前使用的代码完全相同,但现在它说没有随机函数。这是我的程序代码:

from random import *
def rock():
     print " "
     print "-----------------------------------------------"
     print " "
     a = random()
     if a <= 0.33:
         computerChoice = "rock"
     elif a <= 0.66 and a > 0.33:
         computerChoice = "paper"
     elif a > 0.66:
         computerChoice = "scissors"
     userChoice = raw_input("Rock, paper, or scissors: ").lower()
     if userChoice == "rock":
         if computerChoice == "rock":
             print "It's a tie"
             return rock()
         elif computerChoice == "scissors":
             print "You win!"
             return rock()
         elif computerChoice == "paper":
             print "You lose :("
             return rock()
     elif userChoice == "paper":
         if computerChoice == "rock":
             print "You win!"
             return rock()
         elif computerChoice == "paper":
             print "It's a tie"
             return rock()
         elif computerChoice == "scissors":
             print "You lose :("
             return rock()
     elif userChoice == "scissors":
         if computerChoice == "rock":
             print "You lose :("
             return rock()
         elif computerChoice == "paper":
             print "You win~"
             return rock()
         elif computerChoice == "scissors":
             print "It's a tie"
             return rock()
     else:
         print "I don't understand"
         return rock()
 print rock()

为什么这不起作用?我疯了吗?任何输入都有帮助 感谢。

1 个答案:

答案 0 :(得分:0)

您的文件名称不能是random.py。重命名并重试。