0

http://www.cs.uni.edu/~diesburg/courses/cs1510_sp15/homework/PA04/index.htm

So I have this assignment. I have been trying to work out the code to find the additive and multiplicative roots of a user given input. I am new to python and know how to work the problem out, but without the tools (coding know how) have been just running in circles for quite a while now. So if anyone could be so kind as to help me figure out the coding. I have tried slicing the list, but keep getting an error if I try to make the string an int and if I leave it as an string it just doesn't seem to run. I know there is probably a way using modulous as well, but have yet to quite master it.

Thank you for any help any of you are able to leave me.

Edit Here is the code I have so far.

import sys

userStr = input("What number should I use for my \
calculations? ")
userInt = int (userStr)
original = userStr #Save Copy of original value
originalTwo = userStr #Save second Copy of original value

addCount = 0
mulCount = 0

#Stop the program if the integer is less than or equal to 0

while userInt <= 0:
        print ("Thanks for playing along!") 
        sys.exit()


#Use a while loop to repeat the process of splitting an integer into single digits and then adding the individual digits.

print = ("Addition")
while userInt > 9:
    userInt = sum(map(int, userStr))
    print("New Value: ",userStr)
    addCount = addCount + 1


#Use a while loop to repeat the process of splitting an integer into single digits and then multiplying the individual digits.

print = ("Multiplication")    
while original > 9:
    original = (map (int, original))
    print("New Value: ",original)
    mulCount = mulCount + 1

#Print the outputs to the screen for the user.

print("For the Integer: ",userInt)
print("    Additive Persistence= ",addCount,", Additive Root= ", userInt)
print("    Multiplicative Persistence= ",mulCount,", 
      Multiplicative Root= ", original)
nbro
  • 15,395
  • 32
  • 113
  • 196
Kryx
  • 13
  • 6
  • For converting a string to an int: http://stackoverflow.com/questions/379906/parse-string-to-float-or-int – Jon Egeland Feb 27 '15 at 06:09
  • I tried messing with the code from that exact question and it wasn't working when I tried using it. It must be somewhere in my code for adding and multiplying the digits in the integer that is breaking it. Any suggestions? I am using python 3.4 – Kryx Feb 27 '15 at 06:12
  • What **exactly** happened? Vague statements like "it doesn't seem to run" *really* don't help. Also, code samples are *very* effective – Jon Egeland Feb 27 '15 at 06:13
  • I have tried slicing the list, but keep getting an error if I try to convert the string into an integer. Then if I leave it as an string it just doesn't seem to run at all. I just get >> – Kryx Feb 27 '15 at 06:22
  • I edited the post to include my code so far. – Kryx Feb 27 '15 at 06:36
  • Since `original` starts as a string, `while original > 9:` is wrong and should raise. The next line `original = (map (int, original))` if executed, turns `original` into a map object. I suspect you intend to repeatedly multiply the digits of a number the way you added them above, but you do not do that in the code posted. – Terry Jan Reedy Feb 28 '15 at 01:09

0 Answers0