473,699 Members | 2,534 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 1289
"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
2643
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
3405
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
1840
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
2022
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
2390
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
1631
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
3740
by: TheShadow1 | last post by:
safetyTips - this array is in here.js ...
9
1830
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
1478
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
2145
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
8706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8633
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,...
0
9199
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8902
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...
0
7787
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6552
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
5891
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
4642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.