我正在尝试执行这个示例示例脚本,该示例脚本是我在John Zelle撰写的《 Python编程:计算机科学入门》中找到的:

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


我不知道如何解决此问题。有什么建议么?

最佳答案

input总是返回一个字符串:

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


将输入转换为浮点数:

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

关于python - 为什么我会收到TypeError:无法将序列乘以'float'类型的非整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18389727/

10-15 17:56
查看更多