Python Forum

Full Version: Importing a module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I'm learning computer programming with John Zelle's Python Programming: An Introduction to Computer Science. In the first chapter, he explains how to make a module and import it. Following his instructions, I have no difficulties making a module, but I can't import it. When I run 'import', I receive the following error message:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import chaos
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\chaos.py", line 1
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
             ^
SyntaxError: invalid syntax
This is the program I saved:

# File: chaos.py
# A simple program illustrating chaotic behavior.
def main():
    print("This program illustrates a chaotic function")
    x = eval(input("Enter a number between 0 and 1: "))
    for i in range(10):
        x = 3.9 * x * (1 -x)
        print(x)
        main()
I don't know the cause of the error. What could it be?

I use IDLE Python 3.5.2 running on Windows 7.
I am not sure because I am still learning myself but it seems to me your function is calling itself recursively and I don't think that can be right.
(Nov-05-2016, 03:37 PM)Barrowman Wrote: [ -> ]I am not sure because I am still learning myself but it seems to me your function is calling itself recursively and I don't think that can be right.

I invoke the function just fine.
(Nov-05-2016, 03:53 PM)SrirachaSauceLover Wrote: [ -> ]
(Nov-05-2016, 03:37 PM)Barrowman Wrote: [ -> ]I am not sure because I am still learning myself but it seems to me your function is calling itself recursively and I don't think that can be right.

I invoke the function just fine.

How do you invoke it?
As I see it once you call it the last part of the function calls itself which then calls itself ad infinitum.
That is the
 main() after 
print x
As I said I am also still learning
SrirachaSauceLover - It looks like you're trying to run a version of python that you
either haven't installed or is named something other than 'Python35-32' usually it
installs at C:\Python35 check that you actually have the following path:


C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\
(Nov-05-2016, 03:59 PM)Barrowman Wrote: [ -> ]How do you invoke it?
As I see it once you call it the last part of the function calls itself which then calls itself ad infinitum.
That is the
 main() after 
print x
As I said I am also still learning

I just type, "main ()". This, however, might be the problem. Do you know how to position the second 'main ()' directly under 'def main():'? 'main ()' remains in the same line, only more to the left?

(Nov-05-2016, 04:29 PM)Larz60+ Wrote: [ -> ]SrirachaSauceLover - It looks like you're trying to run a version of python that you
either haven't installed or is named something other than 'Python35-32' usually it
installs at C:\Python35 check that you actually have the following path:


C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\

I have this.
You have main() within the def but I don't think it should be there.

I have modified the code somewhat because a copy and paste kept giving an error ( saved it as guest.py )

Quote:  File "./guest.py", line 5

SyntaxError: Non-ASCII character '\xc2' in file ./guest.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
I think your code should be more like this:
#!/usr/bin/env python

def main():
    inp = raw_input("Enter a value between 0 and 1: ")
    x = float(inp)
    for i in range(10):
        x = 3.9 * x * (1 - x )
        print x
        
main() 
It printed out the following:
Quote:/test1$ ./guest.py 
0.975
0.0950625
0.335499922266
0.869464925259
0.442633109113
0.962165255337
0.141972779362
0.4750843862
0.972578927537
0.104009713267
I hope this helps you
First, start by removing that terrible function "eval". It's dripping with pure "evil". Then get rid of the call to main() within main(), there's no reason for nonsense like that.

Does it at least run? What's the filename, and what's the script you're using to import it?
(Nov-05-2016, 06:14 PM)Barrowman Wrote: [ -> ]I think your code should be more like this:
#!/usr/bin/env python

def main():
    inp = raw_input("Enter a value between 0 and 1: ")
    x = float(inp)
    for i in range(10):
        x = 3.9 * x * (1 - x )
        print x
        
main() 
Could you tell me how you put the second "main()" so it's in line with "def main():" ? I think that's the solution. When I press <Enter> after I type "print(x)", it lowers the cursor directly beneath it. The <Tab> doesn't help me either.

By the way, the code isn't mine; it's from Zelle's book.

(Nov-05-2016, 06:18 PM)nilamo Wrote: [ -> ]First, start by removing that terrible function "eval".  It's dripping with pure "evil".  Then get rid of the call to main() within main(), there's no reason for nonsense like that.

Does it at least run?  What's the filename, and what's the script you're using to import it?

Yes, it runs. The filename is "chaos.py". I import it with "import", thought Zelle mentions other methods, and they don't work either.
Quote:the code isn't mine; it's from Zelle's book.

If your using a tutorial that puts in eval() in from direct input of a user, and does not inform you of the security risks...get a new tutorial.
Pages: 1 2 3