I am trying to pass 10 inputs(names) into a list in python but I am getting one list element only take a look!
I tried to do this, but didn't work!
my problem is just at passing the inputs into a list i tried a lot but cant seem to find a solution, please help me with this
for inputs in range(0, 10):
names=input("enter names: ")
students= []
students.append(names)
print(students)
but the output I am getting is:
['example name']
What should I do?
First you should make a list. Then get inputs 10 times and append them into the list ten times. (One name each time)
students = []
for i in range(10):
name = input("enter one name: ")
students.append(name)
print(students)
Or you can have this inline version:
students = [input("enter one name: ") for i in range(10)]
print(students)
If you want to input all the names in one input, suppose that you seperate the names with , character, as much as you want:
students = input("enter names: ").split(',')
This should help you:
students= []
for inputs in range(0, 10):
name=input("enter name: ")
students.append(name)
print(students)
you are overriding you own value of names in the for loop, and then insert it only once, after the loop, to the list. append should be in the loop
students= []
for inputs in range(0, 10):
names=input("enter names: ")
students.append(names)
print(students)
The names variable is a variable, not a list... So with every input, it replaces the text that u entered before. To fix this just instantly append them;
students= []
for inputs in range(0, 10):
students.append(input("enter name: "))
print(students)
Related
In this code, I have a user-generated list of numbers and have to find the amount of duplicates a specific element has within that list. I am getting an error in the function. How do I fix this?
def count(list,y):
new_list = []
for j in range(0,x):
if(list[j]==y):
new_list.append(list[j])
else:
pass
print(new_list)
length = len(new_list)
print("The number {} appears {} times in the list".format(y,length))
list = []
x = int(input("Please enter the size of the list you want to create: "))
for i in range(0,x):
value = input("Please enter value of list : ")
list.append(value)
print("The list of the values you entered : {}".format(list))
y = int(input("Which element do you want to find the number? : "))
count(list,y)
There were multiple issues in your code.
In the loop in function count instead j you are using i as index.
initiation of loop index till range(0,x) => x is not defined as the variable is not assigned in this scope, instead use len of the list.
All the inputs added to the list were strings and the one that was searched was an integer.
Other suggestions:
do not use list as a variable name as it is a keyword.
Below this code I am also providing a shorter version of the function count.
def count(mylist,y):
new_mylist = []
for j in range(0,len(mylist)):
print(mylist[j])
if(mylist[j]==y):
new_mylist.append(mylist[i])
else:
pass
length = len(new_mylist)
print("The number {} appears {} times in the mylist".format(y,length))
mylist = []
x = int(input("Please enter the size of the mylist you want to create: "))
for i in range(0,x):
value = int(input("Please enter value of mylist : "))
mylist.append(value)
print("The mylist of the values you entered : {}".format(mylist))
y = int(input("Which element do you want to find the number? : "))
count(mylist,y)
Shorter version
def count(mylist,y):
length = mylist.count(y)
print("The number {} appears {} times in the mylist".format(y,length))
one issue, you're trying to acces the i'th element in list, but i is not initialized. Try replacing i with j
for j in range(0,x):
if(list[i]==y):
new_list.append(list[i])
If you don't mind me taking liberties with your code, here's an example using the Counter from collections. Note that it doesn't do exactly the same thing as your code, as Counter doesn't use indexes as you were using before.
from collections import Counter
input_counter = Counter()
while True:
value = input("Please enter a value (or nothing to finish): ")
if value == '':
break
input_counter[value] += 1
print(input_counter)
y = input("Which number do you want to count the instances of? ")
print(input_counter[y])
I based my code on Duncan Betts' codes for my assignment but I don't understand it and it seems like it has been 3 years since he last logged on so I can't ask him.
Can you please explain where did the "studentrecord" code come from? What is it?
num = int(input("How many students?: "))
physics_students = [[input("Input student name: "),float(input("Input grade: "))] for studentrecord in range(num)]
physics_students.sort(key=lambda studentrecord: float(studentrecord[1]))
lowest_grade = physics_students[0][1]
ind = 0
while physics_students[ind][1] == lowest_grade:
ind += 1
second_lowest_grade = physics_students[ind][1]
second_lowest_students = []
while physics_students[ind][1] == second_lowest_grade:
second_lowest_students.append(physics_students[ind][0])
ind += 1
if ind == num:
break
second_lowest_students.sort()
print(*second_lowest_students, sep="\n")
Thank you so much for your help!
The two occurrences of studentrecord refer to 2 different things
In the list comprehension, studentrecord is used to hold each element of the range range(num). It's basically an index, but it's never used anyways.
Edit: I don't think the list comprehension should call it studentrecord because the elements of that range are indices and not lists representing a student's name and grade. It's a little confusing, and the variable should probably be renamed to something like i or _.
That list comprehension is like doing this:
physics_students = []
for studentrecord in range(num):
physics_students.append([input("Input student name: "),float(input("Input grade: "))])
or this:
physics_students = []
for studentrecord in range(num):
physics_students[studentrecord] = [input("Input student name: "),float(input("Input grade: "))]
In the lambda expression, studentrecord is the name of the parameter to your anonymous function. It's like saying this:
def my_lambda(studentrecord):
return float(studentrecord[1]
physics_students.sort(key=my_lambda)
def main():
for row in range (7):
assignment = int(1)
if row == 1:
for assignment_number in range(0,8):
assignment_number+1
for i in range(0,7):
assignment_mark = float(input(("Please enter your mark for assginment" assignment_number,": "))
assignment_weight = float(input("Please enter the total weight percentage for the assignment: "))
main()
So this is my code above,
I'm basically trying to work out how I could say for each input variable "Please enter your mark for assignment x (from 1 up to 7).
Which will loop, so once they enter it for assignment 1, it then asks the same question for assignment 2.
I hope this makes some sense. I'm new to programming in general and this just happens to also be my first post on stack! Be gentle (:
Thanks!
There are a few problems with your code:
assignment_number+1 without assigning it to a variable does nothing, and even if you did, that value would be lost after the loop. If you want to offset the numbers by one, you can just use range(1, 8) or do +1 when you actually need that value of that variable
in your second loop, your loop variable is i, but you are using assignment_number from the previous loop, which still has the value from the last execution, 7
you have to store the values for assignments_mark and assignment_weight somewhere, e.g. in two lists, a list of tuples, or a dict of tuples; since assignment numbers start with 1 and not 0, I'd recommend a dict
You can try something like this, storing the marks and weights for the assignments in a dictionary:
assignments = {}
for i in range(7):
assignment_mark = float(input("Please enter your mark for assginment %d: " % (i+1)))
assignment_weight = float(input("Please enter the total weight percentage for the assignment: "))
assignments[i+1] = (assignment_mark, assignment_weight)
print(assignments)
Let the loop do the counting, then use string formatting.
And you only need a single loop to collect each pair of events
from collections import namedtuple
Assignment = namedtuple("Assignment", "mark weight")
assignments = []
for idx in range(7):
print("Please enter data for assignment {}".format(idx+1))
mark = float(input("mark: "))
weight = float(input("weight:"))
assignments.append(Assignment(mark, weight))
print(assignments)
Python gives me the index numbers instead of the input in the list. I'm trying to get the user to give input and then have it store that into a printed list. How do I fix it?
number_of_grades = int(input('How many quizzes have you had?'))
grade_list = []
for grades in range(number_of_grades):
input('Please input a grade from the quiz.')
grade_list.insert(0, grades)
print(grade_list)
It works just fine up until it needs to be printed.
It's because you are not assigning your second input to a variable!, then you are just inserting the index used in the for loop.
number_of_grades = int(input('How many quizzes have you had?'))
grade_list = []
for grades in range(number_of_grades):
value = input('Please input a grade from the quiz.')
grade_list.insert(0, value)
print(grade_list)
I want the program to allow you to input 5 student names and student marks. However every time I run it, it doesnt accept the names of the students.
students=[]
for num in range(5):
x=input("Enter name of students: ")
students.append(x)
marks=[]
for num in range(5):
y=input("Enter marks of students:")
marks.append(y)
report = input("Do you want to print class report?: ")
if report == 'yes':
print(x[0],":", y[0])
print(x[1],":", y[1])
print(x[2],":", y[2])
print(x[3],":", y[3])
print(x[4],":", y[4])
Here is some working code:
n = 5
students=[]
for num in range(n):
x=raw_input("Enter name of students: ")
students.append(x)
marks=[]
for num in range(n):
y=raw_input("Enter marks of students:")
marks.append(y)
report = raw_input("Do you want to print class report?: ")
if report == 'yes':
for i,j in zip(students, marks):
print(i,':', j)
Explanation
Several issues resolved:
Indentation is crucial in Python.
Use list.append on variables that are actually defined.
Use a loop with zip to cycle through pairs of [student, mark] data.
Use raw_input instead of input in Python 2.x.
while Name != "":
i.append(x)
will loop (and append) forever if Name != ""... (and what is i and Name?)
and in python 2.7 you should use raw_input:
a quick fix would be this:
for num in range(5):
x=raw_input("Enter name of students: ")
if x == "":
break
students.append(x)
although, why have a break condition and a loop up to 5 at the same time? why not just
for num in range(5):
x=raw_input("Enter name of students: ")
students.append(x)
Not clear if that code would run as written anyway. Name and i are never defined.
In Python2, you need to use raw_input to accept input, otherwise, what you type will be evaluated internally by Python
Also, you can combine the loops to collect tuples rather than build parallel lists
students=[]
for num in range(5):
x=raw_input("Enter name of students: ")
y=raw_input("Enter marks of students:")
students.append((x,y,))
report = raw_input("Do you want to print class report?: ")
if report == 'yes':
for x, y in students:
print(x,":", y)