LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 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: Reputation: 30
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.
 
Old 09-23-2006, 05:49 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
You aren't reassigning total within your while loop. If total<high_enough is true after the second number, it's always true, so you're stuck in an infinite loop.
 
Old 09-23-2006, 05:57 PM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
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
?
 
Old 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: Reputation: 30
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.
 
Old 09-23-2006, 06:53 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
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.)
 
Old 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: 718

Rep: Reputation: 72
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
 
Old 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: Reputation: 30
Thanks for the help, its appreciated. The tutorial had not covered anything like that yet (that I know of).

Thanks again

Michael.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change the port number of an application tijo.thomas Linux - Kernel 1 08-23-2006 03:45 AM
Python: Change precision of a number Neruocomp Programming 1 02-03-2006 12:57 AM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM
How do I add two octal number ? Linh Programming 3 05-20-2004 03:08 PM
Python problems trying to generate a random number davholla Programming 0 10-27-2003 04:07 AM


All times are GMT -5. The time now is 03:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration