Python Addition Program

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

Re: Python Addition Program

Post by thedotmaster on Sun Jul 19, 2009 3:14 pm
([msg=26980]see Re: Python Addition Program[/msg])

insomaniacal wrote:Instead of trying to do it using less than and greater than operands, you can make it much easier by using
while total != 100:


That wouldn't work if the total was to exceed 100. For example, if the total was 98 and the user inputted 5.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Re: Python Addition Program

Post by htomeht on Tue Aug 04, 2009 7:20 pm
([msg=27895]see Re: Python Addition Program[/msg])

I love python so I thought I would add a few points..

* You never have to set add = 0 since you dont do any checks against it before setting it later in the code
* You shouldnt add and then remove from total if it was to high. Try to only set a value if it will come into the range you want it to be after the setting.
* It is possible to use \n in strings to add newlines. This is more of a style thing, many people prefer the explicit prints

Here is a quick piece of code of how I would write it.
Of course I dont have all your prints in this simplified version but it shows how to bring together the logic.

Code: Select all
#! /bin/env python

total = 0
print "\nLets play a game!"
while total < 100:
    try:
        add = int(raw_input("\nTotal is %s, add another number: " % total))
        if total+add <= 100: total += add
        else: print "\nTotal must not be greater than 100"
    except ValueError: pass
print "\nYou won! Total is %s" % total


-- Wed Aug 05, 2009 1:22 am --

Also it might be worth noting that I totally forgot about negative integers. You can pass -1000 to this program.

Code: Select all
if 0 < total+add <= 100: total += add # This should fix it
Last edited by htomeht on Wed Aug 05, 2009 3:34 am, edited 1 time in total.
htomeht
New User
New User
 
Posts: 7
Joined: Tue Aug 04, 2009 1:42 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by sanddbox on Tue Aug 04, 2009 8:01 pm
([msg=27899]see Re: Python Addition Program[/msg])

I would recommend you don't allow it to go over 100...this could be implemented easily doing this.

ill write this in pseudo-code btw. (english basically)
while completed != 1
{
if a < (100-total)

print "good job!"

*add a to total*

if a > (100-total)

print "screw you stupid asshole, your number puts the value over 100"

if total == 100

print "the total has reached 100. Asshole."!

completed = 1
}

That way you cannot go over 100.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Python Addition Program

Post by thedotmaster on Sat Aug 08, 2009 8:06 am
([msg=28009]see Re: Python Addition Program[/msg])

I'd do it like this:
Code: Select all
try:
  total = 0
  while 1:
    try:
      a = int(raw_input("Enter number: "))
      assert total+a <= 100 and total+a >= 0
    except ValueError:
      print "Error - should be a number"
      # Add exit(1) in here to end if a duff value is given.
      a = 0
    except AssertionError:
      print "Over 100, final value was: %i" % total
      exit(1)
    else:
      total += a
except KeyboardInterrupt:
  print "\rProgram ended. Final value was: %i" % total


For a little program all that exception handling is pretty unnecessary, but it's good to know. That should
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Previous

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests