I need to add all the characters in a entered isbn how would i do that? (python)

StackOverflow https://stackoverflow.com/questions/21724965

  •  10-10-2022
  •  | 
  •  

質問

start = " "
multiplynum = 11
protectedisbn = " "
while start.lower() != "n":
print("Welcome to the isbn number protector!")
start = input ("Would you like to protect your isbn number? (y/n) ")
if start == "y":
    isbn  = input("Please input your 10 digit isbn number. ")
    if len (isbn) != 10:
        print("I am sorry but you need to enter a 10 digit isbn.")
    for ch in isbn:

this is where I am stuck I need help in order to get them all to add together

役に立ちましたか?

解決

ans = sum([int(i) for i in isbn if i.isdigit()])
print ans

Should work for you

The above is the same as saying

ans = 0
for i in isbn:
    if i.isdigit():
        ans += int(i)
print ans
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top