그래서 파이썬으로 코딩하기 시작했고 몬테카를로 알고리즘을 사용하여 파이의 값을 근사값으로하는 프로젝트를 할당 받았습니다. 나는 이미 개념적 비트를 가졌지 만 이제는 사각형을 인쇄하고 그것에 체크 표시를해야합니다. 사각형은 사용자가 지정한 크기로 정의해야합니다.사각형 만들기
나는 다음과 같은 코드를 사용하여 인쇄 광장을 얻을 관리했습니다 : 내가 추가 할 때 어떤 이유를 들어
import random
#defines the size of the square
squareSize = raw_input("Enter a square size:")
#defines the width of the square
print "#" * (int(squareSize)+2)
#defines the length of the square.
for i in range(0,int(squareSize)):
print "#", " " * (int(squareSize)-2), "#"
print "#" * (int(squareSize)+2)
을 :
#determines the x value of a point to display
x = random.uniform(-1*(squareSize),squareSize)
놨어요 변수를 생성
또는 다른 어떤 "squareSize"는 다음과 같습니다.
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "/lib/python2.7/random.py", line 357, in uniform
return a + (b-a) * self.random()
TypeError: unsupported operand type(s) for -: 'str' and 'str'
내가 할 수있는 도움을 주시면 감사하겠습니다. 이걸로, 나는 그것이 내가 바라보고있는 것이 어리석은 것이 틀림 없다라고 확신한다. 그러나 나는 그것을 이해할 수 없다.
감사합니다.
알렉스.
에
raw_input
에 의해 반환 된 문자열을 변환하여, 당신의squareSize
변수 (유형 변경) 캐스트 할 수 있습니다 당신은 random.uniform에서 "INT"를 잊었다 :'random.uniform (-1 * int (squaresize), int (squaresize))). 그러나 @alecxe가 암시 하듯이이 작업은 한 번만 수행해야합니다. – fredtantini