1

私はPython(および一般的なプログラミング)にまったく慣れていないため、Pythonプログラミングの例に従っています:コンピューターサイエンスの紹介John M. Zelle、Ph.D. バージョン 1.0rc2 Fall 2002 明らかに、これはかなり古くなっています。私は Python 3.3 を使用しています。本に示されているとおりに (print ステートメントの周りに () を追加して) 演習を入力していますが、エラーが発生し続けています。これは、私が入力した内容と、プログラムを実行しようとしたときの結果のコピーです。私は何を間違っていますか?

>>> 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()
This program illustrates a chaotic function.
Enter a number between 0 and 1:1
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    main()
  File "<pyshell#11>", line 5, in main
    x= 3.9*x*(1-x)
TypeError: can't multiply sequence by non-int of type 'float'
>>> 
4

1 に答える 1

4

x=float(input ("Enter a number between 0 and 1:"))として使用するとinput()、Python3kでは文字列が返されませんfloat

>>> def main():
...     print ("This program illustrates a chaotic function.")
...     x=float(input ("Enter a number between 0 and 1:"))
...     for i in range(10):
...         x= 3.9*x*(1-x)
...         print (x)
... 
>>> main()
This program illustrates a chaotic function.
Enter a number between 0 and 1:.45
0.96525
0.13081550625
0.443440957341
0.962524191305
0.140678352587
0.47146301943
0.971823998886
0.106790244894
0.372005745109
0.911108135788
于 2012-10-18T16:03:41.440 に答える