首页 > 解决方案 > 我怎样才能对元组求和

问题描述

我必须编写游戏“帐户很好”而为此,我只有它:


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 !!!!!


标签: pythonfunctionoperators

解决方案


我认为英语不是你的第一语言。但是 python 有一个 sum 函数,它接受一个元组作为参数并返回其成员的总和。

tuple=(1,2,3,4)

x=sum(tuple)

print x


推荐阅读