是否有更简单的方法可以添加被认为是字符串的数字,但也可以通过python添加整数?它不允许我添加它,可能是因为我将整数转换为字符串然后是列表的方式?
到目前为止我已经这样做了:
function_menu()
print()
numbers = str(number)
lists = []
lists.extend(numbers)
addition = sum(lists)
average = addition / len(lists)
print(average)
答案 0 :(得分:0)
使用列表理解你可以做这样的事情
addition = sum([int(num) for num in lists])
将您的号码转换为单独数字列表的简单方法是
digits = [int(digit) for digit in str(number)]
# average then becomes
average = sum(digits)/len(digits)
答案 1 :(得分:0)
只需将您的个别字符串数字转换为整数,然后再将它们转换为列表:
numbers = [int(digit) for digit in str(number)]
addition = sum(numbers)
average = addition / len(numbers)
# etc.