Pick a random number in the Python list and add it

Asked 6 days ago, Updated 6 days ago, 1 views

import random
import math
import numpy as np

print("What number(n) would you like to simulate?>> ")
n = int(input())
arr1 = []
A = (1,3,5,6,7,8,9,10)
nx = 0

for i in range(n):
    result = 0
    for _ in range(14):
        ax = int(random.choice(A))
        score = ax + nx
        result += score
        arr1 = (ax + nx)
        nx = ax
        arr1.append(result)


print(arr1)

In this way, I want to write a code that randomly picks up 14 numbers in the list A and adds them over and over again, but I'm not sure if this code is right 'int' object has no attribute 'append' I thought it would be introduced if I put on the int, but I don't think that's it. I ask for your help me

python

2022-09-20 14:37

1 Answers

There seems to be a lack of study on basic grammar.

The append command is a command used for a list object, but you are using it for an int object.

And why is the object declared as the first list designated as an int object in the middle?

The last object you want to output is also being reset per loop. If you're going to use this, it won't be a problem, but... If you plan to continue using it, please study basic grammar.

import random

n = int(input("how many times(n) would you like to simulate?>> "))
result = 0

for i in range(n):
    for _ in range(14):
        ax = int(random.choice(A))
        result += ax

print(result)


2022-09-20 14:37

If you have any answers or tips


© 2022 pinfo. All rights reserved.