使用python查找数字的加法和乘法根

时间:2015-02-27 06:04:58

标签: python-3.x

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

所以我有这个任务。我一直试图找出代码来找到给定输入的用户的加法和乘法根。我是python的新手,知道如何解决问题,但是没有工具(编码知道)已经在圈子中运行了很长一段时间。所以,如果有人能够帮助我弄清楚编码的话。我已经尝试过切片列表,但是如果我尝试将字符串设置为int并且如果我将其保留为字符串,则它仍然会出现错误,它似乎无法运行。我知道也许有一种使用modulous的方法,但还没有完全掌握它。

感谢您的帮助,任何人都可以离开我。

修改 这是我到目前为止的代码。

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)

0 个答案:

没有答案