2014-10-10 2 views
-3

왜 함수에서 randrange를 사용할 수 없습니까? 내 자신의 함수에서 임의의 라이브러리 함수 사용 python 3

import random 

print (random.randrange(0,4)) 

def random(dice): 
    dice_one = random.randrange(1,3) 
    return (dice_one) 


print (random(4)) 

오류를

+7

모듈'random'을 자신의 함수로 대체했기 때문에'random'이라는 이름이 붙었습니다 - 왜? – jonrsharpe

+0

나는 무작위를 대체 할 것임을 깨달았다. 나는 내가 그것에 덧붙일 것이다라고 생각했다. 나는 내가 지금 잘못하고있는 것을 본다. 감사합니다 – user3715055

답변

0

파이썬은 당신이 그것을 무작위로 같은 이름 때문에 당신이 당신의 자신의 임의 함수의 메소드를 호출하려는 생각 어떤 도움

line 6, in random 
    dice_one = random.randrange(1,3) 
    AttributeError: 'function' object has no attribute 'randrange' 

감사입니다 기준 치수. 함수 이름을 다른 것으로 변경하면 문제가되지 않습니다.

def chance(dice): 
    dice_one = random.randrange(1,3) 
    return dice_one 

print (chance(4)) 

또한 'dice'인수로 수행하려는 작업을 확실하게 알지 못합니다. 이것은 다이에의면 수로 의미한다면, 시도 :

def chance(dice): 
    dice_one = random.randrange(1,dice) 
    return dice_one 
0

재미, 실제로 당신이 기대하지만, 어떤에서 나는 프로그램의 솔루션을 이해 출력의 종류를 이해할 수 없습니다입니다 이 !

import random 

    print random.randrange(0,4) 

    def random_number(dice): 
     dice_one = random.randrange(1,3) 
     return dice_one 

    print random_number(4) 
관련 문제