Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 1st, 2005, 2:14 PM   #1
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Help please!

Hi! If I, for exampel, enter the number 123456 is there any way that I can make python read it as 1,2,3,4,5,6?And thus for eampel give the numbers the names a,b,c,d,e,f and then "print a+b+c+d+e+f" and get the answer 21?
lingon is offline   Reply With Quote
Old May 1st, 2005, 2:18 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
What you're asking for is actually very simple. However, I would suggest using an array, and splitting the initial string into single-digit numbers. Have fun.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 1st, 2005, 2:30 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6

print a,",",b,",",c,",",d,",",e,",",f
print "The sum of all those numbers is: %s"%(a+b+c+d+e+f)

a = "1"
b = "2"
c = "3"
d = "4"
e = "5"
f = "6"

x = [a,b,c,d,e,f]
print ",".join(x)
x = int(a)+int(b)+int(c)+int(d)+int(e)+int(f)
print "The sum of all those numbers is:", x

print "1, 2, 3, 4, 5, 6"
print "The sum of all those numbers is: 21"

There are actually a million more ways to do this.

Last edited by Sane; May 1st, 2005 at 2:33 PM.
Sane is offline   Reply With Quote
Old May 1st, 2005, 2:51 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
import string

str = raw_input("Enter some numbers: ")
numbers = string.split(str, " ", 1)
for number in numbers:
    print str(number) + " ",
I think that should do it.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 2nd, 2005, 1:50 PM   #5
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Hmm...What I'm actually trying to do is to make a program that uses a special algorithm to calculate the last number of your person number.
Ex.
881221 0759 This is my number
212121 2121 This is the algorithm. You here take 8*2,8*1,1*2 and so on untill you get the number(in this case):
16821410710 Now you do:
1+6+8+2+1+4+1+0+7+1+0 = 31 Now you take the higest nearest ten and minus it with the number you got:
40-31 = 9 Wich is correct
Can anyone make a program for this?
It sure beats me, but then I guess I suck at this :p

Last edited by lingon; May 2nd, 2005 at 2:22 PM.
lingon is offline   Reply With Quote
Old May 2nd, 2005, 3:31 PM   #6
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
You are not going to get better if you don't try. I wrote the program, it's not too hard. Try it and post any problems you are having. I will start you off:
pernum = '8812210759'
for digit in pernum:
   print digit
You might also want to check out the int() and str() functions.
Moldz is offline   Reply With Quote
Old May 3rd, 2005, 8:47 AM   #7
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Okej, so now it splits it into 9 different numbers but it doesn't understand that they are actually numbers.
Ex. if you print "print digit*2" it prints 88 instead of 16 and 22 instead of 4.
So how do I make it understand that it is numbers not letters? I tryed some different int() things but they didn't work
lingon is offline   Reply With Quote
Old May 3rd, 2005, 9:02 AM   #8
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
Here's what I did in the python console:
>>> digit = "5"
>>> type(digit)
<type 'str'>
>>> number = int(digit)
>>> type(number)
<type 'int'>
That should help.
Moldz is offline   Reply With Quote
Old May 8th, 2005, 11:41 AM   #9
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Okej, thanks. I'll see if I can make it work
lingon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC