Posts: 7
Threads: 3
Joined: Feb 2022
Write a Python program through the following steps:
1. Create a list variable, names, which contains the names of three of your friends.
2. Create a Numpy array, ages, that contains the ages of your friends in the same order that you entered their names in names variable.
3. Use np.random to create a 1-by-3 array of random numbers, and store it in variable rnd.
4. Find the index (position) of the maximum value of rnd and store it in a variable named max_rnd.
5. Change the element of names in the position max_rnd to your name, and the element of ages in the position max_rnd to your age.
Posts: 1,838
Threads: 2
Joined: Apr 2017
Do you have a question? We aren't going to do your homework for you. You must have learnt relevant things in the bootcamp, so you need to apply those.
Posts: 7
Threads: 3
Joined: Feb 2022
(Feb-13-2022, 09:42 AM)ndc85430 Wrote: Do you have a question? We aren't going to do your homework for you. You must have learnt relevant things in the bootcamp, so you need to apply those.
This is my question and a part of my homework. My homework has 20 questions and I answered 15 of them. I had challenges with these five and even I did these five but I got errors. Besides that, this forum is about "Homework". It means that, If we have any question in our homework, we can ask in here and the other help us.
Posts: 582
Threads: 1
Joined: Aug 2019
(Feb-13-2022, 09:53 AM)Raselrameda Wrote: It means that, If we have any question in our homework, we can ask in here and the other help us. Sure we will help you. Post the script you made and show the error message (compete). Then we will explain what went wrong and how to do it right.
ndc85430 and buran like this post
Posts: 1,358
Threads: 2
Joined: May 2019
Yes, but being a homework forum we are careful not to just do your homework for you. We are happy to help you with your homework, but you won't learn anything if we just hand you a solution.
How do you create a list named "names"? How do you populate that list with 3 friend names?
Posts: 7
Threads: 3
Joined: Feb 2022
Feb-14-2022, 07:29 AM
(This post was last modified: Feb-14-2022, 10:31 AM by Larz60+.)
(Feb-13-2022, 01:23 PM)jefsummers Wrote: Yes, but being a homework forum we are careful not to just do your homework for you. We are happy to help you with your homework, but you won't learn anything if we just hand you a solution.
How do you create a list named "names"? How do you populate that list with 3 friend names?
I did it by myself and now, my program doesn't any errors but I think I have some mistakes but I'm as a beginner and I don't know them.
import numpy as np
name = ['Anna', 'Stan', 'Sarah']
age = [25, 45, 37]
array = np.array([[name],[age]])
print(array)
print(type(array))
a = np.random.random((1,3))
print(a)
b = np.max(a)
print(b)
name = ('Anna', 'Stan', 'Sarah')
age = [25, 45, 37]
print(name, age)
name = ['Anna', 'Stan', 'Sarah']
age = [25, 45, 37]
x = list(name)
y = list(age)
x[1] = 'Rasel'
y[1] = '22'
name = tuple(x)
age = tuple(y)
print(name, age)
Larz60+ write Feb-14-2022, 10:31 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBcode tags on future posts.
Posts: 582
Threads: 1
Joined: Aug 2019
Well done, now we can talk. I must say I have no experience with Numpy, but I will try wat I can do.
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 1. Create a list variable, names, which contains the names of three of your friends. import numpy as np
name = ['Anna', 'Stan', 'Sarah'] Very well. But the name of the variable should be "names" as instructed, not "name". Be precise.
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 2. Create a Numpy array, ages, that contains the ages of your friends in the same order that you entered their names in names variable. age = [25, 45, 37]
array = np.array([[name],[age]])
print(array)
print(type(array)) That is not what was asked. The Numpy array should only contain ages. Also: do not use "array" as a variable name because it has a meaning in Numpy. So it should be:
ages = np.array([25, 45, 37])
print(ages)
print(type(ages)) Output: [25 45 37]
<class 'numpy.ndarray'>
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 3. Use np.random to create a 1-by-3 array of random numbers, and store it in variable rnd. a = np.random.random((1,3))
print(a) Output: array([[0.63648152, 0.40100596, 0.03060516]])
You create a variable "a" but it had to be named "rnd". Also: the result appears to be a list in a list (see the square brackets). I would prefer to do it like this:
rnd = np.random.random(3)
print(rnd) Output: array([0.90007612, 0.12248973, 0.08769738])
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 4. Find the index (position) of the maximum value of rnd and store it in a variable named max_rnd. b = np.max(a)
print(b) No. You must not have the max value, but the index position of the max value. So there are more steps.
largest = np.max(rnd)
print(largest)
position = np.where(rnd == largest)
print(position)
max_rnd = position[0][0]
print(max_rnd) Output: 0.9000761230393026
(array([0]),)
0
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 5. Change the element of names in the position max_rnd to your name, and the element of ages in the position max_rnd to your age. name = ('Anna', 'Stan', 'Sarah')
age = [25, 45, 37]
print(name, age)
name = ['Anna', 'Stan', 'Sarah']
age = [25, 45, 37]
x = list(name)
y = list(age)
x[1] = 'Rasel'
y[1] = '22'
name = tuple(x)
age = tuple(y)
print(name, age) Now you lost control. First you are redefining "name" as a tuple (parentheses instead of square brackets) and then you start juggling with tuples and so on.
Obvious you only have to do this:
names[max_rnd] = "Rasel"
ages[max_rnd] = 22
print(names, ages) Output: ['Rasel', 'Stan', 'Sarah'] [22 45 37]
I hope this helps you.
Posts: 7
Threads: 3
Joined: Feb 2022
(Feb-14-2022, 11:36 AM)ibreeden Wrote: Well done, now we can talk. I must say I have no experience with Numpy, but I will try wat I can do.
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 1. Create a list variable, names, which contains the names of three of your friends. import numpy as np
name = ['Anna', 'Stan', 'Sarah'] Very well. But the name of the variable should be "names" as instructed, not "name". Be precise.
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 2. Create a Numpy array, ages, that contains the ages of your friends in the same order that you entered their names in names variable. age = [25, 45, 37]
array = np.array([[name],[age]])
print(array)
print(type(array)) That is not what was asked. The Numpy array should only contain ages. Also: do not use "array" as a variable name because it has a meaning in Numpy. So it should be:
ages = np.array([25, 45, 37])
print(ages)
print(type(ages)) Output: [25 45 37]
<class 'numpy.ndarray'>
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 3. Use np.random to create a 1-by-3 array of random numbers, and store it in variable rnd. a = np.random.random((1,3))
print(a) Output: array([[0.63648152, 0.40100596, 0.03060516]])
You create a variable "a" but it had to be named "rnd". Also: the result appears to be a list in a list (see the square brackets). I would prefer to do it like this:
rnd = np.random.random(3)
print(rnd) Output: array([0.90007612, 0.12248973, 0.08769738])
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 4. Find the index (position) of the maximum value of rnd and store it in a variable named max_rnd. b = np.max(a)
print(b) No. You must not have the max value, but the index position of the max value. So there are more steps.
largest = np.max(rnd)
print(largest)
position = np.where(rnd == largest)
print(position)
max_rnd = position[0][0]
print(max_rnd) Output: 0.9000761230393026
(array([0]),)
0
(Feb-13-2022, 08:55 AM)Raselrameda Wrote: 5. Change the element of names in the position max_rnd to your name, and the element of ages in the position max_rnd to your age. name = ('Anna', 'Stan', 'Sarah')
age = [25, 45, 37]
print(name, age)
name = ['Anna', 'Stan', 'Sarah']
age = [25, 45, 37]
x = list(name)
y = list(age)
x[1] = 'Rasel'
y[1] = '22'
name = tuple(x)
age = tuple(y)
print(name, age) Now you lost control. First you are redefining "name" as a tuple (parentheses instead of square brackets) and then you start juggling with tuples and so on.
Obvious you only have to do this:
names[max_rnd] = "Rasel"
ages[max_rnd] = 22
print(names, ages) Output: ['Rasel', 'Stan', 'Sarah'] [22 45 37]
I hope this helps you.
Thank you so much for your help.
|