G
G
GeighT2020-05-10 22:57:41
Python
GeighT, 2020-05-10 22:57:41

I don't understand how return .?

I wanted to make a characteristic generator for the game, I need to return the characs variable after the chars function, but the program does not display the sum of the three largest numbers from the list.

Вот код : 
from random import *

def chars(a):
    char = [randint(0,6),randint(0,6),randint(0,6),randint(0,6),randint(0,6)]
    c1 = max(char)
    char.remove(c1)
    c2 = max(char)
    char.remove(c2)
    c3 = max(char)
    char.remove(c3)
    c1 = int(c1)
    c2 = int(c2)
    c3 = int(c3)
    print(a)
    characs = c1 + c2 + c3
    return(characs)
    chars(1)
    print(characs)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deleting Account, 2020-05-10
@GeighT

import random

def chars():
   char = [random.randint(0, 6) for number in range(0)]
   max_numbers = []
   count = 0
   
   def get_max_numbers(last_index=0):
         nonlocal count 
         count += 1
         number = max(char[last_index:])
         max_numbers.append(number)
         if count < 3:
             return get_max_numbers(numbers.index(number, last_index))
         return max_numbers   
   return get_max_numbers()

print("".join(chars(), " "))

If with your code then:
from random import *

def chars(a):
    char = [randint(0,6),randint(0,6),randint(0,6),randint(0,6),randint(0,6)]
    c1 = max(char)
    char.remove(c1)
    c2 = max(char)
    char.remove(c2)
    c3 = max(char)
    char.remove(c3)
    c1 = int(c1)
    c2 = int(c2)
    c3 = int(c3)
    print(a)
    characs = c1 + c2 + c3
    return(characs)

print(chars())

Return returns a value, that is, if we write return a + b in the add (a, b) function, then the function will return the sum of numbers a and b, and we can already display or assign this sum to a variable, for example sum = add (1,2) // 3 or print(add(1,2)) // prints 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question