[英]typeerror: can't multiply sequence by non-int of type "float
I am brand new to python (and programming in general) and I am following examples from Python Programming: An Introduction to Computer Science John M. Zelle, Ph.D. 我是python的新手(以及一般的编程),我正在学习Python编程的例子:计算机科学概论John M. Zelle,Ph.D。 Version 1.0rc2 Fall 2002 Obviously this is quite a bit out of date and I am using Python 3.3 I am typing in the exercise exactly as the book shows it (adding () around the print statements) but I keep coming up with the error. 版本1.0rc2 2002年秋季显然这已经过时了,我正在使用Python 3.3我在练习中输入完全与书中显示的一样(在打印语句周围添加())但我不断提出错误。 Here is a copy of what I input and the result when I tried to run the program. 这是我输入的内容的副本以及我尝试运行程序时的结果。 What am I doing wrong? 我究竟做错了什么?
>>> 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'
>>>
use x=float(input ("Enter a number between 0 and 1:"))
as input()
returns a string in python 3k not float
: 使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.