Appending an array to an array using a function in python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to append an array to an array using a function but it seems to return None.
Code (now works):
import random
arr = []
def randomarr():
t = []
for i in range(3):
r = random.randint(1,9)
t.append(r)
return t
for i in range(3):
arr.append(randomarr())
print(matrix)
Edit: Solved by adding a return. Shame on me for forgetting to put a return statement in my code.

Your funtion randomarr needs a return staterment.
try:
def randomarr():
t = []
for i in range(3):
r = random.randint(1,9)
t.append(r)
return t

randomarr() is not retuning anything, is just printing t.
use return like this:
import random
arr = []
def randomarr():
t = []
for i in range(3):
r = random.randint(1,9)
t.append(r)
return t
for i in range(3):
arr.append(randomarr())
print(arr)

Related

random.choice() not selecting all possibilities [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
random.choice() in Python does not work correctly.
I have the following function, but the following happens when called:
def Randomswitch():
thechosenone = random.choice(range(0, 2))
if (thechosenone == 0):
return "WIN"
if (thechosenone == 1):
return "LOSE"
Randomswitch()
When Randomswitch is called it only returns WIN every time it's called.
I am breaking my head trying to figure this out.
Can anyone help me please?
Seems to be working pretty well, have you tried only a few times? Let's try 10,000 times and count the occurrences:
import random
from collections import Counter
def Randomswitch():
thechosenone = random.choice(range(0, 2))
if (thechosenone == 0):
return "WIN"
if (thechosenone == 1):
return "LOSE"
Counter(Randomswitch() for i in range(10000))
output:
Counter({'LOSE': 4980, 'WIN': 5020})
Seems pretty decent ;)
improving the code
That said, you code can be improved, why don't you just pass the values to chose from to random.choice?
def Randomswitch():
return random.choice(['WIN', 'LOSE'])
try this:
import random
def Randomswitch():
range_list = [i for i in range(0, 2)]
thechosenone = random.choice(range_list)
if (thechosenone == 0):
return "WIN"
if (thechosenone == 1):
return "LOSE"
Try use random.randint(0,1) instead of random.choice(range(0,2))

function that produces a list with range as parameter [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
why do the following lines of code:
def every_three_nums(start):
lst = []
for x in range(start,100,3):
lst.append(x)
return lst
when asking print(every_three_nums(91)) just returns 91 as output
thank you in advance for your support
This is because your return is in the for, when a return is called the function ends, try this :
def every_three_nums(start):
lst = []
for x in range(start,100,3):
lst.append(x)
return lst
Wrong indentation of return, so it will returen a list with just one element [91].
Actually you don't need that function, see below
def every_three_nums(start):
lst = []
for x in range(start,100,3):
lst.append(x)
return lst
print(every_three_nums(91))
print(list(range(91,100,3)))
The output is
[91, 94, 97]
[91, 94, 97]

How can I alter the output from my list and sum? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am having a problem with the output my code is giving me and I am stumped. The output should look like this:
1,2,3,4 = 10
But instead, I am getting this:
[1,2,3,4] = 10
Can anyone review my code and tell me why its outputting that?
Here it is:
def sumList(NumList):
NumList = [int(input('Enter the number: ')) for i in range(4)]
print(NumList, "=" ,sum(NumList))
sumList(4)
NumList is a list object, which is denoted by the squared brackets, that's why when you print it shows [1,2,3,4].
If you want to print it like 1,2,3,4 = 10 you will need to treat it first with something like
NewList = [str(i) for i in NumList]
NewList = ','.join(NewList)
then your final function should be
def sumList(NumList):
NumList = [int(input('Enter the number: ')) for i in range(4)]
NewList = [str(i) for i in NumList]
NewList = ','.join(NewList)
print(NewList, "=" ,sum(NumList))

basic question about python for loop and enumerate [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to build a function that will return the sum of the first item in the data list and every tenth item after, so I write the script below, however, the result is always the first data in my list. How should I modify my code and fix it?
def Sum10th(data):
sum=0
for i,d in enumerate(data):
if (i % 10 == 0): sum = sum+d
return sum
p = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
print(Sum10th(p))
The desired result should be 31, however the computer only returns the value of 1.
You can try
p = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
def Sum10th(data):
return sum([v for i, v in enumerate(p) if (i + 1) % 10 == 0 or i == 0])
print(Sum10th(p))
Output
31
By using the list comprehension with an if, at the end of it, you can get all the first item with the tenth item following it from a list.
Try this way:
def sum10thdata(data):
sum = 0
l = len(data)
for i in range(0,l,10):
sum+=data[i]
return sum

Python access return tuple from imported function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Hi i'm having trouble trying to access the values(tuples) from the function which i wrote in another file:
test.py:
from random import randint
def generate_eth():
eth_address = ''.join(["{}".format(randint(0, 9)) for num in range(0, 6)])
eth_private_key = ''.join(["{}".format(randint(0, 9)) for num in range(0, 6)])
return eth_address, eth_private_key
def generate_evt():
evt_address = ''.join(["{}".format(randint(0, 9)) for num in range(0, 6)])
evt_private_key = ''.join(["{}".format(randint(0, 9)) for num in range(0, 6)])
return evt_address, evt_private_key
Each function return 2 values which each are randomly generate string of 6 numbers
And on my main.py:
from test import generate_eth, generate_evt
eth_address, eth_private_key = generate_eth
evt_address, evt_private_key = generate_evt
print(eth_address)
print(eth_private_key)
print(evt_address)
print(evt_private_key)
When i tried to print it i got the following error:
cannot unpack non-iterable function object
I think that i can access return tuple from function like so. What am i doing wrong ?
you need to do the following:
eth_address, eth_private_key = generate_eth()
evt_address, evt_private_key = generate_evt()
The () is missing.

Categories

Resources