473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wrong with this script?

Hi, I've been busy with an experimental script, and I can't seem to
see what is wrong with it, can somebody tell me?

Here it is:

a = 0
b = 1
mainloop = 1

print "Welcome to pyFibo"
print "For more information type \'help\'"
while mainloop==1:
limit = input("Until what number do you want to see the
Fibonacci series?")
if limit=="help":
print "The Fibonacci series is a worldfamous series of
numbers.\
Each consecutive number is calculated by adding the previous two
numbers to\
each other."
else:
while b < limit:
print b
a, b = b, a+b
print "Want to do another series?"
again = input("(Type yes for another series, or anything
else to quit.)"
if again!="yes":
mainloop = 0

Any help is appreciated, thanks ^_^
Jul 18 '05 #1
4 1273
"R.Meijer" <mi********@gmail.com> wrote in message
news:ba**************************@posting.google.c om...
Hi, I've been busy with an experimental script, and I can't seem to
see what is wrong with it, can somebody tell me?

Here it is:

a = 0
b = 1
mainloop = 1

print "Welcome to pyFibo"
print "For more information type \'help\'"
while mainloop==1:
limit = input("Until what number do you want to see the
Fibonacci series?")
if limit=="help":
print "The Fibonacci series is a worldfamous series of
numbers.\
Each consecutive number is calculated by adding the previous two
numbers to\
each other."
else:
while b < limit:
print b
a, b = b, a+b
print "Want to do another series?"
again = input("(Type yes for another series, or anything
else to quit.)"
You need to close the () for input here. After doing that, if you run it
you will notice that you get an exception for most inputs, including "yes".
IIRC, input() is scheduled for removal in some future version of Python
because it doesn't do what you would expect and it is generally a bad idea
to use it. The functionality is along the lines of:

eval(raw_input('your string here'))

You undoubtedly want raw_input() instead here.
if again!="yes":
mainloop = 0


This last line needs indented.

And a couple of minor points:

1. Choose an amount of indentation per level and stick to it. 4 is rather
common in Python code.

2. When posting to the list, make sure that the lines in your code are short
enough that they will not wrap and be posted as broken code. 70 chars is
usually safe.

Daniel Fackrell

Jul 18 '05 #2
Daniel Fackrell <unlearned <at> gmail.com> writes:

You need to close the () for input here. After doing that, if you run it
you will notice that you get an exception for most inputs, including "yes".
IIRC, input() is scheduled for removal in some future version of Python
because it doesn't do what you would expect and it is generally a bad idea
to use it. The functionality is along the lines of:

eval(raw_input('your string here'))

You undoubtedly want raw_input() instead here.
if again!="yes":
mainloop = 0


This last line needs indented.

And a couple of minor points:

1. Choose an amount of indentation per level and stick to it. 4 is rather
common in Python code.

2. When posting to the list, make sure that the lines in your code are short
enough that they will not wrap and be posted as broken code. 70 chars is
usually safe.

Daniel Fackrell


Thank you very much for the help and the tips :-) This is my very first python
script, and I knew it would have some stupid mistakes; but it's doing something
weird right now...I did all the stuff you told, me, and now it'll at least run.
But whenI enter a number as a limit, the loop keeps going on forever, nad the
numbers won't stop rolling. I'm guessing this is because it sees limit as a
string, how can I let it see it as an integer?
Jul 18 '05 #3
R.Meijer wrote:
Hi, I've been busy with an experimental script, and I can't seem to
see what is wrong with it, can somebody tell me?


For future notice, it's useful to

(1) explain what it is you want your script to do, and
(2) explain what it currently does (including an exception traceback if
one is printed)

Using my mind-reading powers, I'd suggest that maybe you want something
like:

py> for limit in iter(lambda: raw_input('What number? '), ''):
.... if limit == "help":
.... print "The Fibonacci series..."
.... else:
.... a, b = 0, 1
.... limit = int(limit)
.... while b < limit:
.... print b
.... a, b = b, a+b
....
[... I type '6' ...]
1
1
2
3
5
[... I type '13' ...]
1
1
2
3
5
8
[... I type '' (nothing) ...]
py>

STeVe
Jul 18 '05 #4
"R.Meijer" <mi********@gmail.com> wrote in message
news:lo**********************@post.gmane.org...
Thank you very much for the help and the tips :-) This is my very first python script, and I knew it would have some stupid mistakes; but it's doing something weird right now...I did all the stuff you told, me, and now it'll at least run. But whenI enter a number as a limit, the loop keeps going on forever, nad the numbers won't stop rolling. I'm guessing this is because it sees limit as a string, how can I let it see it as an integer?


My mistake. I was only looking at the last input() call you were using.
For the other one, when you change it to raw_input(), you will get a string
that you must convert to an integer in order to use it for numerical
calculations or comparisons.

int(raw_input('your message here'))

will do this.

After you make this change, try entering a string that cannot be parsed as
an integer, and you will see another exception (ValueError) is raised. In
order to properly handle this, I would wrap the int(raw_input()) in a try:
except: block inside a loop. When you get a valid integer, you can then
"break" out of the loop and continue executing.

You may also want to look at the rest of your script for another place you
can use "break" in order to eliminate a flag.

Happy scripting, and welcome to the bliss that is Python.

Daniel Fackrell

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bartosz Wegrzyn | last post by:
I need help with sessions. I createt set of web site for nav with authorization. first I go into main.php which looks like this: <?php //common functions include_once '../login/common.php';...
10
by: Greener | last post by:
Hi, I need help badly. Can you do client-side programming instead of server-side to capture the Browser type info? If this is the case, what's wrong with the following? <script...
5
by: tin | last post by:
<script language="Javascript"> <!-- function apri (theURL,winName,features){ window.open (theURL,winName,features); var a=null; oldwindow = window.self; oldwindow.opener = window.self;...
6
by: Rtritell | last post by:
Please can you find out what's wrong, fix the script and tell me what was wrong. Im just beginning <html> <head> <title>Random Mad Lib!</title> <script language="JavaScript"> <!-- Hide
3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
62
by: TheShadow1 | last post by:
safetyTips - this array is in here.js ...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
6
by: plemon | last post by:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.