2017-10-03 14 views
0

나는 평균의 코드와의 거리 만들기에 끝났어요 이후 :파이썬 거북이 그래픽에 IntergerMath를 넣어하려고

x1=eval(input("Please insert a first number: ")) 
y1=eval(input("Please insert a second number: ")) 
x2=eval(input("Please insert a third number: ")) 
y2=eval(input("Please insert a fourth number: ")) 
add = x1 
add = add + y1 
add = add + x2 
add = add + y2 
average = add/4 
d= distanceFormula(x1,y1,x2,y2) 
print("Average:", average) 
print("Distance:", d) 

지금 현재와 막대 그래프에 intergermath를 연결하는 그래픽을 추가하는 작업입니다을 파이썬 거북이 그래픽. 그러나, 나는이 코드 (입력)를 입력하고있어 몇 가지 문제에 걸쳐 온 :

def doBar(height, clr): 
    begin_fill() 
    color(clr) 
    setheading(90) 
    forward(height) 
    right(90) 
    forward(40) 
    right(90) 
    end_fill() 

y_values = [str(y1), str(y2)] 
x_values = [str(x1), str(x2)] 
colors= ["red", "green", "blue", "yellow"] 
up() 
goto(-300, -200) 
down() 
idx = 0 
for value in y_values: 
    doBar(value, colors[idx]) 
    idx += 1 

를 그리고 여기가 정상으로 나가서 후 나는 약간의 오차가있어 출력의 결과입니다 :

Traceback (most recent call last): 
in main 
doBar(value, colors[idx]) 
in doBar 
forward(height) 
line 1637, in forward 
self._go(distance) 
line 1604, in _go 
ende = self._position + self._orient * distance 
line 257, in __mul__ 
return Vec2D(self[0]*other, self[1]*other) 
TypeError: can't multiply sequence by non-int of type 'float' 

그래서 여기서는 평균값과 거리 값을 모두 입력으로 사용하려고합니다. 사용자가 4 개의 숫자를 입력하도록 요청해야하며, 출력은 파이썬 거북이 그래픽에 4 개의 막대를 그립니다.

그럼이 코드가 그래픽에서 작동하도록하려면 어떻게해야합니까?

+0

지금까지 사용하지 마십시오을'평가 (입력())'. 이건 위험 해. –

+0

eval (input())은 얼마나 위험합니까? –

+0

[매우!] (https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) 임의의 악성 코드가 실행됩니다 (예 : https://stackoverflow.com/a/37081082/5067311). –

답변

0

heightdoBar()으로 문자열으로 전달됩니다. 함수는 문자열을 forward() 함수에 전달하지만 정수 또는 부동 소수점이 필요합니다.

y 방향 값

은 여기에 문자열로 변환됩니다

y_values = [str(y1), str(y2)] 

당신은 str() 변환을 제거하여 문제를 해결할 수 있습니다

y_values = [y1, y2] 

doBar() 삼각형이 아닌 사각형을 그립니다. 그것은 사각형의 오른쪽에 대한 인출 할 길이 height 또 하나 개의 수직 라인이 필요 :

def doBar(height, clr): 
    begin_fill() 
    color(clr) 
    setheading(90) 
    forward(height) 
    right(90) 
    forward(40) 
    right(90) 
    forward(height) # draw back down to the x-axis 
    end_fill() 
+0

모든 새로운 문제 : 당신이 그 문제를 해결하는 데 도움을 주었음에도 불구하고, 원래 생각했던 것처럼 두 개의 삼각형을 그리지 만 막대 만 만들 수는 없다는 결론을 얻었습니다. –

+0

결과는 다음과 같습니다 : https://i.stack.imgur.com/Djzvv.png –

+0

@ AntonO.Obyedkov :'doBar()'함수를보십시오. 'height'를 한 줄 긋기 한 다음 오른쪽으로 40 줄을 그어 멈추고 채 웁니다. 삼각형을 만들 수 있도록 2 줄 밖에 없습니다. 채우기 전에 바의 오른쪽을 그리기 위해'forward (height)'에 또 다른 호출을 추가해야합니다. – mhawke