I want my results to show only once, but it keeps repeating the last result adding to new result making a pyramid. How do i fix this please! so for example when i run the code i want it to be like this: [(12.44, 4.18), (9.57, 3.03), (3.14, 0.46), (13.46, 4.58)], etc, etc
NOT LIKE THIS:
[(12.44, 4.18)] [(12.44, 4.18), (9.57, 3.03)] [(12.44, 4.18), (9.57, 3.03), (3.14, 0.46)] [(12.44, 4.18), (9.57, 3.03), (3.14, 0.46), (13.46, 4.58)]
def choco_and_nobel(minimum_, maximum_):
pair_list = []
pair_list2 = []
for i in range(0,50):
c = np.round(np.random.uniform(minimum_, maximum_),2)
n = np.round(np.float32(0.4*c-0.8),2)
if (n < 0):
n = 0
pair_list.append(c)
pair_list2.append(n)
ordered_pairs = list(zip(pair_list,pair_list2))
print(ordered_pairs)
return(pair_list)
print(choco_and_nobel(0,15))
Correct Code:
import numpy as np
def choco_and_nobel(minimum_, maximum_):
pair_list = []
pair_list2 = []
for i in range(0,50):
c = np.round(np.random.uniform(minimum_, maximum_),2)
n = np.round(np.float32(0.4*c-0.8),2)
if (n < 0):
n = 0
pair_list.append(c)
pair_list2.append(n)
ordered_pairs = list(zip(pair_list,pair_list2))
print(ordered_pairs)
return(pair_list)
print(choco_and_nobel(0,15))
There was just a minute error in the given code. It was printing in the pyramid format as printing of ordered_pairs was done inside the for loop. Everytime a new value was inserted in the list, the printing was done. To display the final result, that is, after 50 iterations, the printing has to be done outside the for loop.
Error:
import numpy as np
def choco_and_nobel(minimum_, maximum_):
pair_list = []
pair_list2 = []
for i in range(0,50):
c = np.round(np.random.uniform(minimum_, maximum_),2)
n = np.round(np.float32(0.4*c-0.8),2)
if (n < 0):
n = 0
pair_list.append(c)
pair_list2.append(n)
ordered_pairs = list(zip(pair_list,pair_list2))
print(ordered_pairs) #error
return(pair_list)
print(choco_and_nobel(0,15))
Correction:
import numpy as np
def choco_and_nobel(minimum_, maximum_):
pair_list = []
pair_list2 = []
for i in range(0,50):
c = np.round(np.random.uniform(minimum_, maximum_),2)
n = np.round(np.float32(0.4*c-0.8),2)
if (n < 0):
n = 0
pair_list.append(c)
pair_list2.append(n)
ordered_pairs = list(zip(pair_list,pair_list2))
print(ordered_pairs) #correction
return(pair_list)
print(choco_and_nobel(0,15))
For further doubts or questions do comment below.
Get Answers For Free
Most questions answered within 1 hours.