Python Addition Program

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Python Addition Program

Post by To0f0oT on Fri Jul 10, 2009 2:36 am
([msg=26591]see Python Addition Program[/msg])

I Barely started learning python and have been trying to do an exercise I found in a tutorial which tells me to "Write a program that continually reads in numbers from the user and adds them together until the sum reaches 100."

So far I have this and it only works for the first 3 numbers you enter because I don't know the shortcut to having it request and adding more input and I'm definitely not going to keep writing the same lines over and over infinite amount of times, Partly because I'm lazy but mostly because its impossible lol. Oh and if anyone wants to hear something humorous it took me like 5 minutes to find out where to put my code :oops: embarrassing.

So if anyone has any suggestions on how I could use a shortcut or better organize my code or any helpful tips whatsoever they will be greatly appreciated. :mrgreen:

Code: Select all
print "This program will allow you to add numbers until you reach 100."

# Variables
# a=first number
# b=second number
# c=third number

a = input("Please enter a number less than 100 to begin: ")

while a > 99:
    a = input("It must be less than 100. Try again: ")

if a < 100:
    b = input("Good choice. Now add another: ")

while b+a > 100:
    b = input("Too high, try again: ")

if b+a < 100:
    print "You now have", b+a
    c = input("Add another: ")
    while c+b+a > 100:
        c = input("Too high, try again: ")
    if c+b+a == 100:
        print "Great Job!"

if b+a == 100:
    print "Great Job!"
To0f0oT
New User
New User
 
Posts: 12
Joined: Tue Jul 07, 2009 10:01 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by Repleh on Fri Jul 10, 2009 3:42 am
([msg=26595]see Re: Python Addition Program[/msg])

It's not too difficult. we can use only while loop to solve the problem.
Code: Select all
#!/bin/python
print "This program will allow you to add numbers until you reach 100."

# Variables
# a = total


a = input("Please enter a number less than 100 to begin: ")

while a < 100:
   a += input("It must be less than 100. Try again: ")

print 'The result:',a
print "Great Job!"
Repleh
New User
New User
 
Posts: 1
Joined: Fri Jul 10, 2009 3:34 am
Blog: View Blog (0)


Re: Python Addition Program

Post by To0f0oT on Fri Jul 10, 2009 4:02 am
([msg=26596]see Re: Python Addition Program[/msg])

Am I supposed to add that to my previous code? I tried it on its own and all it does is print the input. Sorry I am extremely noobish at programming.
To0f0oT
New User
New User
 
Posts: 12
Joined: Tue Jul 07, 2009 10:01 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by insomaniacal on Fri Jul 10, 2009 6:59 am
([msg=26598]see Re: Python Addition Program[/msg])

Code: Select all
#!/bin/python

#Vars
total = 0
add = 0

#While Loop

while total < 100:
    add = input("Give me a number less than 100\n")
    total = total + add
    print "Total is currently",total
print "Total is now: ",total
print "That's more than or equal to 100!"


This will work on it's own.

You only need two variables for this, one to store the total, and one to be constantly changed by the user.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Python Addition Program

Post by To0f0oT on Sat Jul 11, 2009 2:39 am
([msg=26629]see Re: Python Addition Program[/msg])

Thanks that's just the shortcut I was looking for :mrgreen: Now I'm trying to make it so the user can't go over 100 and if they do the program goes back to the previous total and tells the user to add a different number. I'm experimenting with everything it seems lol at least I'm getting lots of practice.

edit: so this is what I came up with so far thanks to the help of my HTS family. I managed to make it so the user is not able to enter a number larger than 100 to begin with and I made it so if the user reaches a sum over 100 when adding different numbers it will have them input a different number...almost.

The almost is because my solution to the problem where the user reaches a sum over 100 was creating a new input thingy called new_add as opposed to jut add this is so the program will disregard the number that made the sum over 100 and start from the last sum/number under 100. Th problem is when you enter an incorrect answer (a number that makes the sum greater than 100) and then another incorrect answer of greater value directly after it will start to increase the value of the last sum/number under 100. I hope I gave a good enough explanation if not try it out for yourselves. To do this enter 50 as your first number then enter 51 it will tell you its too high and to try again then enter 52 and then to see what I mean enter 0 and the original value will have risen by 1.

Thanks in advance To0f0oT

Code: Select all
#!/bin/python

# Vars

total = 0
add = 0

# Objective

print "Your goal is to reach 100 using addition. You will begin with 0."
print

# While Loop

while total < 100:
    add = input("Now add a number: ")
    while add > 100:
        add = input("Too high, try again: ")
    total = total+add
    print "Total is now",total
    if total == 100:
        print "Great Job!"

# Needs fixing *specifically new_add solution isn't working*

    while total > 100:
        new_add = input("Too high, try again: ")
        total = total+new_add-add
        print "Total is now",total
        if total == 100:
            print"Great Job!"
To0f0oT
New User
New User
 
Posts: 12
Joined: Tue Jul 07, 2009 10:01 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by insomaniacal on Sat Jul 11, 2009 7:49 am
([msg=26639]see Re: Python Addition Program[/msg])

Instead of trying to do it using less than and greater than operands, you can make it much easier by using

while total != 100:

So you run the loop until the total is equal to 100. In the loop, you just have to add an "if-then" statement to subtract the last number added if the total goes over 100. Heres what I mean, in code form :)

Code: Select all
#!/bin/python
# -*- coding: utf-8 -*-

add = 0
total = 0

add = input("Enter a number less than 100")
if add > 99:
    print "Enter a number less than 100"
else:
    total = total + add
    print "Total is ",total

while total != 100:
  add = input("Enter a number to add to the total: ")
  total = total + add
  if total > 100:
    total = total - add
    print "Please try again, total was over 100. "
  else:
    print "Total: ",total

print "Total is equal to 100. Good Job!!"
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Python Addition Program

Post by To0f0oT on Sat Jul 11, 2009 5:14 pm
([msg=26653]see Re: Python Addition Program[/msg])

Thank you so much! Man it's such a relief to finally end up with something I'm happy with after all that time haha I think it's kinda funny that it took me so long to do a simple program oh well.

If you don't mind I just have a couple of questions left well really one
what do these comments mean? I know they're just comments and have no effect on the program but I'm just curious what they stand for.

#Vars
#!/bin/python
# -*- coding: utf-8 -*-


THANK YOU!

Edit: Alright I lied I have more questions. I showed my dad the program and first thing he did was press enter without entering a number and messed everything up and then when I restarted it he entered a letter instead of a number and messed it up again lol. Does anyone know what I can do in order to make my program be able to handle no input and non numerical input. Things that may help me include: The thing I put that represents all numbers, the thing I put that represents all letters and symbols (not including numbers), and what will make my program keep running if no input is given but enter is pressed. I think if I find out what those things are I could probably just add this to my code right?:
Code: Select all
if add = thing (anything besides numbers):
print blah blah blah


P.S. What do you call the things I'm referring to?
To0f0oT
New User
New User
 
Posts: 12
Joined: Tue Jul 07, 2009 10:01 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by insomaniacal on Sat Jul 11, 2009 9:41 pm
([msg=26668]see Re: Python Addition Program[/msg])

The comments are really just good programming habit.

#!/bin/python is the Path to where Python is installed, I'm on a Linux system, so I generally include that.

#Vars - I just put that in front of where I start defining variables, just a habit I have.

#-*- coding: utf-8 -*- : My text editor added that in because it doesn't use the standard encoding, but rather UTF-8, doesn't really serve any purpose.

------------
As for your other question, you can use exception handlers. For example, to make sure the user enters a number, you could use this:

Code: Select all
while total != 100:
  try:
    add = int(raw_input("Enter a number to add to the total: "))
    break
  except ValueError:
      print "Enter a number!!!"


You could also replace the ValueError exception with one for a blank entry (I forget it off the top of my head). Anyway. you may want to read this :

http://docs.python.org/tutorial/errors.html
For more in depth info on exceptions.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Python Addition Program

Post by To0f0oT on Mon Jul 13, 2009 1:28 am
([msg=26719]see Re: Python Addition Program[/msg])

Thank you I used your recommendation and went to the site and learned a lot.
I am super happy with what I have so far and am finally willing to leave it be until I learn something new haha.
anyways if your in any way curious as to what I came up with here you go.

Thanks to my HTS family and special thanks to insomaniacal

Code: Select all
add = 0
total = 0


print "Your goal is to reach 100 using addition. You will begin with a total of 0."
print


while total != 100:
    try:
        add = int(raw_input("Enter a number to add to the total: "))
        total = total + add
        if total < 101:
            print "Total is now",total
            print
    except ValueError:
        print
        print "Enter whole numbers only!"
        print "Total is now",total
        print
    if total > 100:
        total = total - add
        print
        print "Please try again, total was over 100. "
        print "Total is now",total
        print


if total == 100:
    print "Great Job!"


P.S. Anybody else who thinks they can offer any other kind of tips to a new programmer don't hesitate to mention them :mrgreen:
To0f0oT
New User
New User
 
Posts: 12
Joined: Tue Jul 07, 2009 10:01 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by insomaniacal on Mon Jul 13, 2009 7:33 am
([msg=26727]see Re: Python Addition Program[/msg])

Only tips I can offer to you is to keep on learning about anything that interests you :)

And, you're welcome :D
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests