i made it like this: -
def tvarsumman(n):
-
if n<10:
-
print n
-
else:
-
tvarsumman((n/10) + n%10)
but when it comes to 99 it does this
99--> 9+9=18-->1+8=9
thats wrong according to my professor.
can u help fix my function?
and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
11 15558
one other small problem i have is i need to define a function that takes the number i put in splits it up and adds them together for example:
123 --> 1+2+3=6
88 --> 8+8=16
thanks for all ur help guys u are really saving my life!
<snip>and one other small problem i have is i need to define a function that takes the number i put in splits it up and adds them together for example:
123 --> 1+2+3=6
88 --> 8+8=16
thanks for all ur help guys u are really saving my life!
Last things first (by the way, where my dict functions helpful?): -
>>> user_input = '12345'
-
>>> numList = list(user_input)
-
>>> total = 0
-
>>> for num in numList:
-
... try: # prevent errors in the input from breaking the loop
-
... total += int(num)
-
... except (ValueError, TypeError):
-
... pass # do nothing
-
...
-
>>> print total
-
15
-
>>>
i made it like this: -
def tvarsumman(n):
-
if n<10:
-
print n
-
else:
-
tvarsumman((n/10) + n%10)
but when it comes to 99 it does this
99--> 9+9=18-->1+8=9
thats wrong according to my professor.
can u help fix my function?
and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
You are welcome. I would not have guessed that English was not your first language. However, I am having some trouble following this.
You are welcome. I would not have guessed that English was not your first language. However, I am having some trouble following this.
If recursion is not a requirement, then this may be what you are looking for: -
>>> def SumDigits(n):
-
... numList = list(str(n))
-
... total = sum(int(c) for c in numList)
-
... print total
-
...
-
>>> SumDigits(99)
-
18
-
>>>
i made it like this: -
def tvarsumman(n):
-
if n<10:
-
print n
-
else:
-
tvarsumman((n/10) + n%10)
but when it comes to 99 it does this
99--> 9+9=18-->1+8=9
thats wrong according to my professor.
can u help fix my function?
and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
-
>>> def tvarsumman(n):
-
... total = 0
-
... while n > 0:
-
... total += n%10
-
... n /= 10
-
... return total
-
...
-
>>> print tvarsumman(99)
-
18
-
>>> print tvarsumman(123)
-
6
-
>>>
bvdet 2,851
Expert Mod 2GB
i made it like this: -
def tvarsumman(n):
-
if n<10:
-
print n
-
else:
-
tvarsumman((n/10) + n%10)
but when it comes to 99 it does this
99--> 9+9=18-->1+8=9
thats wrong according to my professor.
can u help fix my function?
and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
Using recursion, you need a way to terminate the function. You can do that by substituting 'print n' with 'return n'. The last line also needs a return, and n%10 needs to be outside of the argument list. Use divmod() to determine the integer quotient and remainder. - def tvarsumman(n):
-
if n < 10:
-
return n
-
else:
-
n, rem = divmod(n, 10)
-
return tvarsumman(n) + rem
Example:
>>> tvarsumman(99)
18
>>> tvarsumman(12345)
15
>>> tvarsumman(987654321)
45
>>> tvarsumman(10135)
10
>>>
Tjo! Gissar att du är svensk ;)
Och att du ska räkna ut tvärsumman med hjälp av en rekursiv funktion.
(Sorry about the language everyone else. And DDCane to if im wrong about his nationality, nothing realy important above)
Anyway: -
def numsum(n):
-
if n > 0:
-
n = n % 10 + numsum(n/10)
-
return n
-
Something like that?
Tjo! Gissar att du är svensk ;)
Och att du ska räkna ut tvärsumman med hjälp av en rekursiv funktion.
Two Swedes on the boards! Very cool. My father was half Swedish, but I never learned the language.
(My apologies if I have mistaken your nationality)
Two Swedes on the boards! Very cool. My father was half Swedish, but I never learned the language.
(My apologies if I have mistaken your nationality)
Nope, Youre entirely correct. I'm from Sweden.
I klicked report insted of reply :/ meh.
Nope, Youre entirely correct. I'm from Sweden.
I klicked report insted of reply :/ meh.
Hey guys! Actually I'm American but I live in Sweden at the moment. Where in Sweden are from?
Hey guys! Actually I'm American but I live in Sweden at the moment. Where in Sweden are from?
Up in the north. Live somewhere in the area around Umeå now.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Spidah |
last post by:
Is there an easier way to get the number of items in a database table than
selecting all the records in it and then running mysql_num_rows on the
result?
Hamilton
www.laughland.biz
|
by: ssmith579 |
last post by:
I'm trying to extract single digit number from a string.
t = '06897'
I want to get the 7, 9,8 & 6 seperated out to use but can't find a way
to split out the single characters.
Thanks
|
by: Shahar |
last post by:
Hi
I need to get a field name 'ID'(that is an auto-number field) right
after I add a new row to table, it's work like that:
myCommand.ExecuteNonQuery();
myCommand.CommandText = "SELECT...
|
by: John Shum |
last post by:
I prepare a UserControl that act as a lookup control, once the code is
enter in the TextBox, the description will appear in another TextBox
outside the UserControl of the same row in a DataGrid via...
|
by: UJ |
last post by:
Is there a way to get the number of items that are in an enum? I'm trying to
find out how many items there are so that I can hit every one of them to
test.
TIA - Jeff.
|
by: amiga500 |
last post by:
Hello,
I have a data grid which can contain as many items as possible in one
of the columns I have Qty. What I want to do is when I run the forum I
want to get the number of items available in...
|
by: FORGET |
last post by:
Can anybody show me how to get the serial number of a pocketpc using VC++?
I can get the PDA name with RegQueryValueEx, but I have no idea what to send for the serial number.
Thanks
|
by: StanislavPetrov |
last post by:
How to get the number of the current line in a RichTexBox?I mean the line under the mouse cursor.
|
by: Gangreen |
last post by:
Visual Basic.net :
is there a way of getting the number of threads?
|
by: jeddiki |
last post by:
I am writing a little script that will improve authors writing skills by
finding repeated phrases in the text.
The text of a chapter will average about 10,000 words, however, I could
reduce the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |