473,807 Members | 2,825 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 1295
"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
2648
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
3417
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
1846
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
2029
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
2400
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
1641
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. *************** index.html ***************************** <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...
62
3776
by: TheShadow1 | last post by:
safetyTips - this array is in here.js ...
9
1838
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 converter)
6
1481
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 = + $25; var v13 = + $75 var v14 = + $100; function f1()
3
2153
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
9599
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6879
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.