-1

I need to make a program that the user will enter in any number and then try guess the sum of those digits.

How do i sum up the digits and then compare then to his guess?

I tried this:

userNum = raw_input("Please enter a number:\n")

userGuess = raw_input("The digits sum is:\n")

if sum(userNum, userGuess):
    print"Your answer is True"
else:
    print "Your answer is False"

and it didnt work

Bite code
  • 578,959
  • 113
  • 301
  • 329
user2955610
  • 815
  • 2
  • 9
  • 15

5 Answers5

2

Assuming you are new to Python and you've read the basics you would use control flow statements to compare the sum and the guess.

Not sure if this is 100% correct, feel free to edit, but it works. Coded it according to his(assuming) beginner level. This is assuming you've studied methods, while loops, raw_input, and control flow statements. Yes there are easier ways as mentioned in the comments but i doubt he's studied map Here's the code;

def sum_digits(n):
    s = 0
    while n:
        s += n % 10
        n /= 10
    return s
sum_digits(mynumber)
mynumber = int(raw_input("Enter a number, ")) 

userguess = int(raw_input("Guess the digit sum: "))
if sum_digits(mynumber) == userguess:
    print "Correct"
else:
    print "Wrong"

Credit to this answer for the method.

Digit sum method in Python

Community
  • 1
  • 1
LotusUNSW
  • 2,027
  • 18
  • 20
2

You have 2 problems here :

  • raw_input() doesn't return an integer, it returns a string. You can't add strings and get an int. You need to find a way to convert your strings to integers, THEN add them.
  • You are using sum() while using + whould be enough.

Try again, and come back with your results. Don't forget to include error messages and what you think happened.

Bite code
  • 578,959
  • 113
  • 301
  • 329
2

the python code is :

def digit_sum(n):
  string = str(n)
  total = 0
  for value in string:
      total += int(value)
  return total

and the code doesnot use the API:

def digit_sum1(n):
  total=0
  m=0
  while n:
      m=n%10
      total+=m
      n=(n-m)/10    
return total
brian tse
  • 147
  • 2
  • 6
1
  1. Firstly you neet to use something such as int(raw_input("Please enter a number:\n")) so the input returns an integer.
  2. Rather than using sum, you can just use + to get the sum of two integers. This will work now that your input is an integer.
notquiteamonad
  • 1,159
  • 2
  • 12
  • 28
1

Basically I would use a generator function for this

It will iterate over the string you get via raw_input('...') and create a list of the single integers

This list can then be summed up using sum

The generator would look like this:

sum([ int(num) for num in str(raw_input('Please enter a number:\n')) ])

Generators create lists (hence the list-brackets) of the elements prior to the for statement, so you could also take the double using:

[ 2 * int(num) for num in str(raw_input('Please enter a number:\n')) ]

[ int(num) for num in str(123) ] would result in [1,2,3]

but,

[ 2 * int(num) for num in str(123) ] would result in [2,4,6]

derLukers
  • 11
  • 1
  • 1