Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Help with python application to add number until 100 or more, new user.
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 09-23-2006, 04:50 PM #1 Michael_aust Member   Registered: Aug 2005 Location: Lancashire (United Kingdom) Distribution: Debian Etch, on 686 machine. Posts: 509 Rep: Help with python program to add number until 100 or more, new user. I am starting out learning python and at present I am following the guide over at http://www.hetland.org/python/instant-hacking.php I am up to the section with the following task: Exercise 1 Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100. Code: ```high_enough = 99 number = input("Please enter a number: ") if number > high_enough: print "This is the end Step1" else: number2 = input("Please enter a second number") total = number + number2 print "The total of these two number is STEP2", total while total < high_enough: number3 = input("Please enter another number") newtotal = total + number3 while newtotal < high_enough: print "The current total is STEP3", newtotal print "This is the end"``` The following works: *Entering the first number as 100 or more causes the program to finish correctly *Entering the first number as less then 100 causes the program to ask for a second number to be entered *Entering a second number that when added to the first number is equal to 100 or more causes the program to quit correctly. However If the first and second numbers do not equal 100 or more then it keeps asking for another number to be entered, even if the third number makes the sum 100 or more. This is the first real time I have tried programming and this one question, that should be pretty simple has had me stumped for days. Could some one point me in the right direction as to what I am doing wrong/missing from the code? I am guessing the fact I have been struggling with this for that last three days is a sign that programming is something I am not going to be any good at, and that I do not have a programmers mentality. Thanks in advance, Michael. Last edited by Michael_aust; 09-23-2006 at 05:22 PM.
 09-23-2006, 05:49 PM #2 Dan04 Member   Registered: Jun 2006 Location: Texas Distribution: Ubuntu Posts: 207 Rep: You aren't reassigning total within your while loop. If total
 09-23-2006, 05:57 PM #3 Dan04 Member   Registered: Jun 2006 Location: Texas Distribution: Ubuntu Posts: 207 Rep: And why not just write something like: Code: ```total = 0 while total < 100: number = input("Please enter a number: ") total += number print "The current total is", total``` ?
 09-23-2006, 06:41 PM #4 Michael_aust Member   Registered: Aug 2005 Location: Lancashire (United Kingdom) Distribution: Debian Etch, on 686 machine. Posts: 509 Original Poster Rep: Dan04: Thanks for that. I dont understand what the: total += number is doing though? If you could do, could you just say what it is actually doing so that I can learn from this. I thought I was doing something wrong given that what I was trying was long while the previous parts of the tutorial were much shorter. I have spent days trying to do this, and all I got was an overly complicated mess *bangs head against wall*. Thanks again.
 09-23-2006, 06:53 PM #5 taylor_venable Member   Registered: Jun 2005 Location: Indiana, USA Distribution: OpenBSD, Ubuntu Posts: 892 Rep: X += Y is shorthand for saying X = X + Y. (In contrast to many other languages, like C and Java, this is only by convention in Python; it's not hardwired into the language. When you get to operator overloading you'll understand, but for now, just assume that X += Y is the same as X = X + Y.)
09-23-2006, 07:06 PM   #6
makyo
Member

Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep:
Hi, Michael_aust.
Quote:
 Originally Posted by Michael_aust I am guessing the fact I have been struggling with this for that last three days is a sign that programming is something I am not going to be any good at, and that I do not have a programmers mentality.
A few observations.

The features, pieces, syntax of a language need to be used over and over to see how they can be put together for a solution to a given problem. Writing the programs and, often, reading others' programs are the best ways to learn a language.

It may be that this language does not suit your style. There are many more languages, some very dense, some very verbose. After you learn one, you'll see that they all have very similar characteristics, but they provide them in different ways. There are perhaps six major categories of languages, so give yourself time to learn this one.

While is is true that some people seem like natural programmers, programming skill comes to most people with practice. I have a rule, makyo's number, that says that you need to do 100 programs before you know the language sufficiently. There are correlations between programming and some other characteristics of humans -- for example, music. Those who like and play music can often be good programmers (not much of a stretch since music theory has much grounding in mathematics). I have known several practicing musicians who were very good programmers -- one had even studied at Julliard.

You have probably lived your life so far as we all do. Nature works in parallel. Most languages work serially (although there has been much work done in recent years of parallelism), so you may need to think very linearly for the time when you are new at this. Logic plays a strong role in programming as you have seen.

I encourage you to stick with it for a while -- as Chevy Chase said, be the ball ... cheers, makyo

 09-24-2006, 01:35 PM #7 Michael_aust Member   Registered: Aug 2005 Location: Lancashire (United Kingdom) Distribution: Debian Etch, on 686 machine. Posts: 509 Original Poster Rep: Thanks for the help, its appreciated. The tutorial had not covered anything like that yet (that I know of). Thanks again Michael.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post tijo.thomas Linux - Kernel 1 08-23-2006 03:45 AM Neruocomp Programming 1 02-03-2006 12:57 AM sethgeekx86 Programming 4 06-10-2004 08:55 AM Linh Programming 3 05-20-2004 03:08 PM davholla Programming 0 10-27-2003 04:07 AM

All times are GMT -5. The time now is 10:00 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -