2013-08-22 2 views
1

나는이 예제 Python 스크립트를 실행하려하고있다. Python Programming : 컴퓨터 과학 개론 by John Zelle :왜 내가 먹는거야 TypeError : 'float'타입의 비 int로 시퀀스를 곱할 수 없다

# File: chaos.py 
# A simple program illustrating chatic behavior 

def main(): 
    print("This program illustrates a chaotic function") 
    x = input("Enter a number between 0 and 1: ") 
    for i in range(10): 
     x = 3.9 * x * (1 - x) 
     print(x) 

main() 

...하지만 어떤 이유로, 나는이 오류가 계속 :

Traceback (most recent call last): 
    File "C:\...\chaos.py", line 11, in <module> 
    main() 
    File "C:\...\chaos.py", line 8, in main 
    x = 3.9 * x * (1 - x) 
TypeError: can't multiply sequence by non-int of type 'float' 

을 나는 방법이 문제를 해결하는 방법 아무 생각이 없습니다. 어떤 제안?

+0

을 시도해보십시오

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

범인은 여기에

documentation입니다 (파이썬 3.x를을 사용하는 것 같은데) 사용 하시겠습니까? 이것은 2.7에서 완벽하게 작동합니다. 그것은 정말로 정말로 jank 번호를 입력 할 때조차도 작동합니다 ... 그렇습니다. 오류를 수행 할 수있게되었지만, 문자열 입력을 시도한 후에 만 ​​...... "0과 1 사이의 숫자를 입력하십시오." 당신이 타이핑하고 있습니까? – TehTris

답변

2

input는 항상 문자열을 반환 :

>>> type(input(":")) 
:a 
<class 'str'> 
>>> type(input(":")) 
:1 
<class 'str'> 
>>> 

은 float로 입력 변환 : 기본적으로

x = float(input("Enter a number between 0 and 1: ")) 
+1

int가 아닙니다. 0에서 1 사이입니다. – user2357112

+0

@ user2357112 - 잘 잡습니다. 내가 어떻게 그것을 놓쳤는지 모르겠다. – iCodez

+0

'>>> X = 입력() >>> X >>> 유형 (x)의 '입력() 당신이 그것을 – TehTris

4

input()이 문자열을 반환합니다. 그것을 사용하기 전에 그것을 float으로 형변환해야합니다.

x = input("Enter a number between 0 and 1: ") 

당신이 파이썬의 버전입니다

x = input("Enter a number between 0 and 1: ") 
x = float(x) 
관련 문제