Help with python application to add number until 100 or more, new user.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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*.
(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.)
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.