Python3.4 - 带索引号的数学
Python3.4 - math with index numbers
我的 objective 是使用列表的索引来做 addition/subtraction。我在哪里将偶数索引变为正数,将奇数索引变为负数。
EX1: 1234508 应由 0 回答:1-2+3-4+5-0+8 = 11,然后 while 再次循环,我得到 1-2+1 = 0
Ex2: 12345 应该由 3 回答:1-2+3-5 = 3,所以它不应该再次通过循环。
Ex3: 121 应由 0 回答:1-2+1 = 0,因此不应再次循环。
def main():
print()
print("Program to determine if a number is evenly\ndivisible by 11")
print()
indexed = input("Enter a number: ",)
total = 0
num = 0
while num >= 10:
for item in indexed:
if num %2 == 0:
total = total + int(item)
else:
total = total - int(item)
num = num + 1
print(total)
main()
请注意,上面的打印语句是 if 语句的占位符,它在我的代码中处于非活动状态,但在此处打印为粗体字。
你到底想用这个做什么?
evenindex = evenindex int(item)
"list"是类型,表示python中的列表类型,所以不能是变量名。此外,您还没有在代码中定义此变量。
假设您有一个字符串 st
,其字符都是数字,并且您想要这些数字的总和。然后定义以下函数
def sd(st):
return sum(int(d) for d in st)
我们可以在解释器中测试
In [30]: sd('10101010101010101010')
Out[30]: 10
In [31]: sd('101010101010101010101')
Out[31]: 11
你真正想要的是奇数相加,偶数相减,但这相当于奇数相加,偶数相加,然后取差,不是吗?所以你想要的是
step_1 = sd(odds(st)) - sd(evens(st))
如何区分奇数位和偶数位?啊!不需要函数,我们可以使用 slices
step_2 = sd(st[::2]) - sd(st[1::2])
现在我们要在解释器中测试切片
In [32]: '101010101010101010101'[::2]
Out[32]: '11111111111'
In [33]: '101010101010101010101'[1::2]
Out[33]: '0000000000'
但是 step_2
可能是负数,我不想管理...我宁愿使用 abs
内置
step_3 = abs(sd(st[::2]) - sd(st[1::2]))
这正是您要找的。
最终我们可以将以上所有内容放在一起,但我们可能需要迭代直到差异小于 11
--- 我们将使用无限循环和 break
语句来找到答案后退出循环
def sd(st):
return sum(int(d) for d in st)
number = input('Give me a number: ')
trial = number
while True:
n = abs(sd(trial[::2]) - sd(trial[1::2]))
if n < 11: break
trial = str(n)
if n > 0:
...
else:
...
我已经找到我上面问的问题的答案了。因此,我在这里的回答是万一有人偶然发现了我的上述问题。
def main():
索引=输入("Enter a number: ",)
total = 0
num = 0
while num <= 10:
for item in indexed:
if num %2 == 0:
total = abs(total + int(item))
else:
total = abs(total - int(item))
num = num + 1
if total == 0:
print(indexed, "is evenly divisible by 11 \ncheck since", indexed, "modulus 11 is", int(indexed) % 11)
else:
print(indexed, "is not evenly divisible by 11 \ncheck since", indexed, "modulus 11 is", int(indexed) % 11)
input()
主要()
我的 objective 是使用列表的索引来做 addition/subtraction。我在哪里将偶数索引变为正数,将奇数索引变为负数。
EX1: 1234508 应由 0 回答:1-2+3-4+5-0+8 = 11,然后 while 再次循环,我得到 1-2+1 = 0 Ex2: 12345 应该由 3 回答:1-2+3-5 = 3,所以它不应该再次通过循环。 Ex3: 121 应由 0 回答:1-2+1 = 0,因此不应再次循环。
def main():
print()
print("Program to determine if a number is evenly\ndivisible by 11")
print()
indexed = input("Enter a number: ",)
total = 0
num = 0
while num >= 10:
for item in indexed:
if num %2 == 0:
total = total + int(item)
else:
total = total - int(item)
num = num + 1
print(total)
main()
请注意,上面的打印语句是 if 语句的占位符,它在我的代码中处于非活动状态,但在此处打印为粗体字。
你到底想用这个做什么?
evenindex = evenindex int(item)
"list"是类型,表示python中的列表类型,所以不能是变量名。此外,您还没有在代码中定义此变量。
假设您有一个字符串 st
,其字符都是数字,并且您想要这些数字的总和。然后定义以下函数
def sd(st):
return sum(int(d) for d in st)
我们可以在解释器中测试
In [30]: sd('10101010101010101010')
Out[30]: 10
In [31]: sd('101010101010101010101')
Out[31]: 11
你真正想要的是奇数相加,偶数相减,但这相当于奇数相加,偶数相加,然后取差,不是吗?所以你想要的是
step_1 = sd(odds(st)) - sd(evens(st))
如何区分奇数位和偶数位?啊!不需要函数,我们可以使用 slices
step_2 = sd(st[::2]) - sd(st[1::2])
现在我们要在解释器中测试切片
In [32]: '101010101010101010101'[::2]
Out[32]: '11111111111'
In [33]: '101010101010101010101'[1::2]
Out[33]: '0000000000'
但是 step_2
可能是负数,我不想管理...我宁愿使用 abs
内置
step_3 = abs(sd(st[::2]) - sd(st[1::2]))
这正是您要找的。
最终我们可以将以上所有内容放在一起,但我们可能需要迭代直到差异小于 11
--- 我们将使用无限循环和 break
语句来找到答案后退出循环
def sd(st):
return sum(int(d) for d in st)
number = input('Give me a number: ')
trial = number
while True:
n = abs(sd(trial[::2]) - sd(trial[1::2]))
if n < 11: break
trial = str(n)
if n > 0:
...
else:
...
我已经找到我上面问的问题的答案了。因此,我在这里的回答是万一有人偶然发现了我的上述问题。
def main(): 索引=输入("Enter a number: ",)
total = 0
num = 0
while num <= 10:
for item in indexed:
if num %2 == 0:
total = abs(total + int(item))
else:
total = abs(total - int(item))
num = num + 1
if total == 0:
print(indexed, "is evenly divisible by 11 \ncheck since", indexed, "modulus 11 is", int(indexed) % 11)
else:
print(indexed, "is not evenly divisible by 11 \ncheck since", indexed, "modulus 11 is", int(indexed) % 11)
input()
主要()