Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing a module
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
(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
Reply
#5
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\
Reply
#6
(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.
Reply
#7
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
Reply
#8
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?
Reply
#9
(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.
Reply
#10
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.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 289 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 895 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,167 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Importing module in jupyter Noteboook ajitnayak1987 0 1,759 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  ERROR: importing desired module mbgamer28 0 1,690 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  importing module - not working jdhamblett 3 3,036 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  importing same python library in multiple custom module escape_freedom13 6 3,844 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  Importing module from a package results in import error goghvv 2 2,410 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Please help: problem installing/importing langdetect module in Jupyter Notebook ledgreve 3 7,310 Dec-30-2019, 08:17 AM
Last Post: LeanbridgeTech
  Problem with importing and using collections module pythomdummy 3 5,824 Nov-14-2019, 08:48 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020