python - 我怎样才能对元组求和
问题描述
我必须编写游戏“帐户很好”而为此,我只有它:
from random import *
# etat = nombre obtenu
randint(0, 20)
nombre1 = randint(0, 200)
nombre2 = randint(0, 200)
nombre3 = randint(0, 200)
nombre4 = randint(0, 200)
nombre5 = randint(0, 200)
nombre6 = randint(0, 200)
nombre_final = randint(1, 1000)
état_initial = (nombre1, nombre2, nombre3, nombre4, nombre5, nombre6)
etat_final = nombre_final
etat = (0, 0, 0, 0, 0, 0)
def gagner(etat):
return etat == etat_final
我的最终目标是让“nombre_final”感谢我的 6 个随机数,但对我来说还很遥远。
我只想创建我所有的运算符,我想知道如何返回 1 个将成为元组结果的数字。
例如数字 1+ 数字 2 = 我的新号码
对于我的运营商,我做到了,但我知道这很糟糕:
def gagner(etat):
return etat == etat_final
def addition1(etat):
return((((etat[0])) + (etat[1])))
def addition2(etat):
return((etat[0] + etat[2]))
def addition3(etat):
return((etat[0] + etat[3]))
def addition4(etat):
return((etat[0] + etat[4]))
I did it for all, and for substraction, multiplication, division...
Thanks in advance !!!!!
解决方案
我认为英语不是你的第一语言。但是 python 有一个 sum 函数,它接受一个元组作为参数并返回其成员的总和。
tuple=(1,2,3,4)
x=sum(tuple)
print x
推荐阅读
- html - Can a github page made using default templates ALSO render a custom html file AS IS from the same repository?
- yii2 - 使用表达式 DBQuery Yii2 选择 SQL 时出错
- android - Open item in new activity from RecyclerView (data from json)
- javascript - Debugging side-loaded add-in running in Word
- javascript - Getting Maximum call stack size exceeded
- reactjs - Hooks 错误:在 Windows 上使用 NextJS 或 ReactJS 的 Hook Call 无效
- python - 来自多个摄像头的 OpenCV 多线程捕获
- moving-average - 使用数组上的滑动窗口移动平均时间序列
- javascript - 并行异步 Mongoose 查询 || eval(return) SyntaxError : 非法返回语句
- c - Why doesn't FindWindowEx find IP Address window?