I want to create a simple app to Ask user to give name and marks of 10 different students and Store them in dictionary.
so far only key:value from the last input is stored into dictionary.
can you please check my code?
marks = {}
for i in range(10):
student_name = input("Enter student's name: ")
student_mark = input("Enter student's mark: ")
marks = {student_name.title():student_mark}
print(marks)
Your current code has two issues: first, it doesn't save the values of each student inside the loop. Second, it always rewrites the entire dictionary with a single key/value pair, that's why it doesn't work.
marks = {}
for i in range(10):
student_name = input("Enter student's name: ")
student_mark = input("Enter student's mark: ")
marks[student_name.title()] = student_mark
print(marks)
You need your code to be inside your loop. Also the way you put value inside your dict is not right. This should do the trick
marks = {}
for i in range(10):
student_name = input("Enter student's name: ")
student_mark = input("Enter student's mark: ")
marks[student_name] = student_mark
print(marks)
Related
userinput = input("Enter your name: ")
names = []
My question is, how to store the input to the list?
I tried to add it like a integer, but it doesn't work.
since names is a list, you want to append the input to it.
userinput = input("Enter your name: ")
names = []
names.append(userinput)
print(names)
names.append(userinput) you this code and should work
If you want to put all answers to a list, you can use append() function.
userinput = input("Enter your name: ")
names = []
names.append(userinput)
int(userinput) makes it an int and [] makes it a seperate list so you can append it instead of adding it.
userinput = input("Enter your name: ")
names = []
names += [int(userinput)]
You could use the append method. Example:
names = []
while True:
name = input("Enter a name or <enter> if you are finished: ")
if name == '':
break
else:
names.append(name)
print(f'You entered {names}')
list1 =["Jack","Donald"]
newname = input("Enter new name: ")
list1.append(newname)
I am trying to code a program that calls on an established class from another python file called student. In the file student, a class called StudentInfo is established and init checks if the data is valid (eg. grade must be between 9-12, course code must fit format, etc.) I am trying to first take the user's inputs here.
import student
import transcript
def add_student(data):
dataDict = data
ID = str(len(dataDict) + 1)
student = StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))
return dataDict
When I try to define student as an object of class StudentInfo, it returns
NameError: name 'StudentInfo' is not defined.
I'm not sure what I'm doing wrong. I thought it might be the inputs but when I removed them it seemed to do the same thing. Please help and thanks in advance.
You need student.StudentInfo if you're using import student.
Alternatively, you can import as:
from student import StudentInfo
To use the code that you have now.
It appears you forgot to prefix StudentInfo with student.
You can either replace:
import student
With:
from student import StudentInfo
Or you can replace:
student = StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))
With:
student = student.StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))
On a side note: You shouldn't name variables after imports. Rename the variable student to something else.
I am trying to figure out how to create a sentinel code to allow the user to input a name and test score that will be listed in a text file. I just need the names to be in column one and scores in column two of the text file. I tried having the name and grade as a single input but was given errors. I am still getting an error for the code below.
Enter the students’ name and test score. Store entered data in a text file named Class.txt, and close the file. Use an empty string as a sentinel to stop the data entry. Make sure that program will accept only correct data for the student’s test scores.
def main():
outfile = open("Class.txt", 'w')
count = 0
student = input("Please enter a student's last name (<Enter> to quit): ")
grade = eval(input("Please enter the student's grade (<Enter> to quit): "))
while student != "" and grade != "":
count = count + 1
student = input("Please enter a student's last name (<Enter> to quit): ")
grade = eval(input("Please enter the student's grade (<Enter> to quit): "))
print(student, grade, file = outfile)
outfile.close()
main()
error:
grade = eval(input("Please enter the student's grade (<Enter> to quit): "))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
You are sending an un-sanitized user input to eval - this is a very unsafe practice, and it is also what's causing an error when you try to use the sentinel (empty input for grade). You should not really be asking for grade if the name is empty.
To avoid repetition, you should place you should place your input calls in a function:
def get_data():
student = input("Please enter a student's last name (<Enter> to quit): ")
if student:
grade = input("Please enter the student's grade (<Enter> to quit): ")
if grade.isdigit():
return (student, int(grade))
return (None, None)
I added an implicit sentinel - if grade is not an natural number, then the loop will stop, too.
Then, you loop would look like this:
(student, grade) = get_data()
while student:
print(student, grade, file = outfile)
(student, grade) = get_data()
Note that I swapped the order of the input and output in the main loop, since in your original code the first input would have not been processed.
Students = {}
def IslemYap():
Input = int(input("Process Number: "))
if Input == 1:
StudentName = input("Student Name: ")
for i in range(1,8):
Students.update({StudentName:[input()]})
print(Students)
IslemYap()
Im trying this but don't working. 7 times per student append grades.
you always overwrite it and never fill the grades in a proper list.
Students = {}
def IslemYap():
Input = int(input("Process Number: "))
if Input == 1:
StudentName = input("Student Name: ")
for i in range(1,8):
Students[StudentName] = Students.get(StudentName,[]) + [input()]
print(Students)
IslemYap()
You can add the grades to a list and simply add it to the correspondent student in the dictionary
Students = {}
def IslemYap():
Input = int(input("Process Number: "))
grades = []
if Input == 1:
StudentName = input("Student Name: ")
for i in range(1,8):
grades.append(input())
Students.update({StudentName: grades})
grades = []
print(Students)
IslemYap()
I'm trying to take information from the user and then take that info to update a dictionary. It's a part of an exercise I'm doing. I've tried to use .format(var1, var2) with the way you update a dictionary, but when ends up happening is only one part of the dictionary gets updated.
name_grade = {}
while True:
name = raw_input("Please give me the name of the student [q to quit]:")
if name == 'q':
break
else:
grade = raw_input("Give me their grade: ")
name_grade['{}'] = '{}'.format(name, grade)
name_grades = {}
while True:
name = input("Please give me the name of the student [q to quit]:")
if name == 'q':
break
else:
grade = input("Give me their grade: ")
name_grades[name]=grade
print(name_grades)
Are you looking for this or something else?
You can update the dictionary by saying
name_grade[name] = grade