473,563 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ 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 1283
"R.Meijer" <mi********@gma il.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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********@gma il.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
2629
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'; global $LOGINDIR;
10
3389
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 language="JavaScript"> function doWord(file) { if (navigator.userAgent.indexOf("MSIE")!=-1)
5
1835
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; oldwindow.close(); } -->
6
2015
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
2385
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."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
7
1623
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 of buttons. the top slideshow (the smiles work fine. How ever the sigs slideshow displays the pictures in the smiles section. ***************...
62
3687
by: TheShadow1 | last post by:
safetyTips - this array is in here.js ...
9
1816
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 have the same code, except different variables. (Note Javascript and htm code was writen by a c program, the code then run thru a html to js...
6
1472
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 http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script type="text/javascript">var v11 = + $0; var v12 = +...
3
2137
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 Record (i) is shown and modified, the change will come to Record (i+1). Can anyone provide suggestion? thanks.
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
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 we have to send another system
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.