好吧,我已经制作了我的代码,以便用户可以输入7个数字,并将它们乘以1表示奇数索引号,3表示偶数:
num = str(input("Please enter 7 numbers")
length = len(num)
while length < 7 or length ? 7:
num = input("Only enter 7 numbers")
string = ''
for t in range(1,8):
if t % 2 == 0:
string += str(t * 3)
else:
string += str(t) + ' '
print(string)
这样可以正常工作,但是现在我需要添加所有数字并将其从最高的10个中取出来,例如,所有数字加起来为53我需要从60中取出它离开我7,将是我的八号,然后我得到那个数字后我打印出来,我如何得到它添加数字并将它从最高的10中取出并将两者的差异输出到我已经拥有的数字中?
由于 布拉德
答案 0 :(得分:0)
我相信这是你正在寻找的:
def take_away_from_nearest(number, nearest):
return nearest - (number % nearest)
用法:
>>> take_away_from_nearest(53, 10)
7
修改强> 如果我理解正确,那将是整个代码:
while True:
# this is just an easy way to keep asking until the input is correct
num = input("Please enter 7 numbers: ")
if len(num) == 7:
break
weird_sum = 0 #here's where we're gonna sum up the numbers; the "eighth number"
for index, character in enumerate(num):
if index % 2 == 0: # index is odd, so count the character thrice
print(3 * int(character))
weird_sum += 3 * int(character)
else: # index is even
print(int(character))
weird_sum += int(character)
print(10 - (weird_sum % 10)) # 10 minus (weird_sum modulo 10)
# and finally, adding them all up and checking whether it ends with 0:
print((10-(weird_sum % 10) + weird_sum) % 10 == 0) # prints True
答案 1 :(得分:0)
如果您的号码x
等于53,那么上升应为math.ceil(x)
,但math.ceil()
的{{1}}轮次除外。为了解释这一点,我们除以10,使用1
,然后再乘10:
math.ceil()
答案 2 :(得分:0)
在第一行缺少括号,这是无效的while length < 7 or length ? 7: