typeerror:不能将序列乘以“int”类型的非int

时间:2012-10-18 16:00:56

标签: python

我是python的新手(以及一般的编程),我正在关注以下示例 Python编程: 计算机科学导论 John M. Zelle,Ph.D。 版本1.0rc2 2002年秋季 显然这已经过时了,我使用的是Python 3.3 我正在键入练习,正如书中所示(在打印语句周围添加())但我不断提出错误。这是我输入的内容的副本以及我尝试运行程序时的结果。我做错了什么?

>>> 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'
>>> 

1 个答案:

答案 0 :(得分:4)

使用x=float(input ("Enter a number between 0 and 1:")),因为input()会返回python 3k中不是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
相关问题