typeerror:无法将sequence乘以类型为“”的nonit

2024-04-20 03:12:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python(以及一般的编程)是全新的,下面是 Python编程: 计算机科学导论 John M.Zelle博士。 版本1.0rc2 2002年秋季 显然这有点过时了,我使用的是python3.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'
>>> 

Tags: andinnumber内容main编程functionbetween
1条回答
网友
1楼 · 发布于 2024-04-20 03:12:56

使用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

相关问题 更多 >