我需要这样做,我想出了这个补充:
import random
mynos = []
print ("First, we'll do some addition")
for counter in range (0,2):
mynumber = random.randint(1,20)
mynos.append(mynumber)
print(mynos)
the_answer = sum(mynos)
#print(the_answer) (^^^)
player_answer =int(input("What is the answer?"))
def main():
if player_answer is the_answer:
print ("Correct! Well done!")
print ("You scored 1 out of 10 so far!")
print ("Good job!")
else:
print ("Incorrect! You scored null points!")
print ("You have zero out of 10 so far!")
exit()
main()
这是该计划的添加部分,但是没有使用sum
来减去mynos
中随机生成的两个数字吗?
答案 0 :(得分:1)
如果你只有2个整数,只需对各个元素使用减法:
the_answer = mynos[0] - mynos[1]
'功能性'减去序列中数字的方法是使用reduce()
function:
import operator
try:
# Python 3 moved reduce to the functools module
from functools import reduce
except ImportError:
pass
the_answer = reduce(operator.sub, mynos)
在这种情况下,operator.sub()
function代表-
运营商。