0

What is wrong with this code that I'm trying to run in Python? (Pretend that indenting isn't an issue):

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()

As you can imagine, this has been pretty confusing since I entered it exactly as my textbook shows it. Thanks for the help!

rakslice
  • 8,742
  • 4
  • 53
  • 57
  • It isn't properly indented, for one. I'd recommend reading [this](http://stackoverflow.com/questions/13884499/what-is-python-whitespace-and-how-does-it-work). – Yuushi Dec 12 '13 at 05:19
  • what is the error you are getting? - i suggest to indent it also properly – mmr Dec 12 '13 at 05:23
  • 1
    EOFError: EOF when reading a line – user111463 Dec 12 '13 at 05:24
  • I suggest to always put the error that you get and python version that you use to help people answer your question. – Mas Adit Dec 12 '13 at 06:43
  • FYI, this code appears in the second edition of *Python Programming: An Introduction to Computer Science* (Zelle), which has switched over to Python 3, hence the use of `input()` instead of `raw_input()`. – rakslice Dec 12 '13 at 23:26

3 Answers3

1

Need to indent the function block

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()

Also, instead of eval I would just use 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()

Sample:

>>> 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: .2
0.624
0.9150336
0.303213732397
0.823973143043
0.565661470088
0.958185428249
0.156257842027
0.514181182445
0.974215686851
0.0979659811419
Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • Sorry about that - I'm new and still need to figure out all the formatting rules. Anyways, this is actually how I have it formatted in my editor and I just made the eval to float change, but I am getting error messages. Do you know how I would fix this? – user111463 Dec 12 '13 at 05:22
  • Make sure there is a line break before `main()` otherwise it will think it is part of the function. I also updated the answer with sample usage. – Matt Williamson Dec 12 '13 at 05:47
1

You are doing two wrong things with your code.
1 : You must not use eval with input method because eval ask for string as input while with input you are returning float value.
You can simply run your program if you are passing float as input.

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

You need to use raw_input(raw_input will return string data)

x = eval(raw_input("Enter a number between 0 and 1: "))

2 :with for loop you need to provide indentation.

Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
  • FWIW `input` is equivalent to `eval(raw_input())`. From the [docs](http://docs.python.org/2/library/functions.html#input) – RedBaron Dec 12 '13 at 06:04
1

I think your problem was because you run it in sublime text editor

Try running it from command line

$ python yourscript.py

and you'll see that your script run normally.

EOFError that you get was caused by sublimtext that didn't send any input to your program while the built-in input function asked for an input.

Mas Adit
  • 166
  • 1
  • 3
  • Looks like [the Sublime Text author confirmed that isn't supported](http://www.sublimetext.com/forum/viewtopic.php?f=3&t=5410), although someone in that post suggests an add-on that might do it (I haven't tried that.) – rakslice Dec 12 '13 at 06:30
  • 1
    yes, there also another question regarding this http://stackoverflow.com/questions/9142290/python-3-1-and-sublime-text-2-error – Mas Adit Dec 12 '13 at 06:41