>>> #File: chaos.py
>>> # A simple program illustrating chaotic behavior.
>>> def main():
... print("This program illustrates a chaotic funciton.")
... x = eval(input("Enter a number between 0 and 1: "))
... for i in range (10):
... x = 3.9*x*(1-x)
... print(x)
I’m supposed to be able to import this file that I saved but I keep getting this error:
import chaos
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
import chaos
File "/Users/nicoledasilva/Documents/chaos.py", line 1
Python 3.12.0 (v3.12.0:0fb18b02c8, Oct 2 2023, 09:45:56) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
^
SyntaxError: invalid decimal literal
Did some research and all I can find is that an invalid decimal literal would indicate that I shouldn’t have a digit in the name, but there isn’t one. The name is chaos. I’m trying to learn with a textbook and self teaching but I can’t figure out why this isn’t working. Any ideas? I’m a newb as it gets, have mercy on me.
That first formatted block is what you’d see when entering something at Python’s interactive prompt, not what you’d put in a Python program.
The traceback in the second formatted block is showing that chaos.py starts with lines that are printed when you’re using Python’s interactive prompt, also not what you’d put in a Python program.
Finally, if you want to convert a string that contains a number to a float, use float, not eval. eval evaluates an entire expression, which is way more than you want it to do most of the time.
Not as shown, no. Things like >>> and ..., and the line shown in your error message (which is also in your file, or the error message couldn’t know about it), are not part of Python syntax. (Well, the ... can have meaning, but this isn’t how it’s used.)
(Actually, this error message is interesting for me, because I don’t have 3.12 installed yet. There are many reasons why this line of text is not valid Python code, but an error like this can only report the first thing that goes wrong, and then stop everything. In previous versions of Python, a different problem would be “noticed first”.)
If you saw an example like this in a book, on a webpage or other reference, it is not showing you how to produce a Python code file. Instead, it is showing you what you would see in the terminal, if you use the Python interpreter to input and try out code.
Because the error message says <pyshell#6>, I guess that you are using IDLE to try the code. In IDLE, the window titled “IDLE Shell 3.12.0” is equivalent to that interpreter. Markings like this are shown in that window, but they are not supposed to be in the .py file that you save. The point of those markings is to help you understand what’s code, what’s input and output from that code, and (at the start) what version of Python you are using. But the .py file needs to only contain code.
So, the corresponding source code file would only contain:
# A simple program illustrating chaotic behavior.
def main():
print("This program illustrates a chaotic funciton.")
x = eval(input("Enter a number between 0 and 1: "))
for i in range (10):
x = 3.9*x*(1-x)
print(x)
But there are still some problems here.
First, as noted, you should use float to convert the input to a floating-point number, not eval. eval lets the user type in a Python code expression, which means (with a bit of careful planning) making your program do anything the user wants it to do that Python can do.
Reference:
Second, even fixing that, the user could type something that doesn’t make sense as a floating-point number, which would cause that to crash. You will probably want to detect this problem, and try asking the user again until the input is valid.
References:
Finally, after fixing those problems, you won’t see anything happen when you import the module. It won’t ask you for a number or do any math. This is because nothing has actually called the main function; a function only defines (hence def) something that can be done. In Python, the name main is not special like it is in some other languages; the function only gets called if you call it.