![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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?
|
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
import string
str = raw_input("Enter some numbers: ")
numbers = string.split(str, " ", 1)
for number in numbers:
print str(number) + " ", |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
Here's what I did in the python console:
>>> digit = "5" >>> type(digit) <type 'str'> >>> number = int(digit) >>> type(number) <type 'int'> |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
Okej, thanks. I'll see if I can make it work
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|