Question

Help needed related python task ! Thanx again How you're doing it • Write a function...

Help needed related python task ! Thanx again

How you're doing it
• Write a function write_to_file() that accepts a tuple to be added to the end of a file
o Open the file for appending (name your file 'student_info.txt')
o Write the tuple on one line (include any newline characters necessary)
o Close the file
• Write a function get_student_info() that
o Accepts an argument for a student name
o Prompts the user to input as many test scores as they wish
o Stores the information (name and scores) in a tuple
o Calls the function write_to_file() sending the tuple to be written to the end of the file
• Write a function read_from_file() that
o Reads a file line by line
o Prints each line to the console
• In main
o Call the get_student_info() for at least 4 different students.
o Call read_from_file()
Notes/Hints
• Because tuples are immutable, you'll want to store your data in a tuple that has a list nested inside of it.
o ('name',[score1,score2,score3,etc.])
o You'll be able to edit the list inside the tuple. In the example above, you could append to the list by doing something like this: score_list[1].append(student_score)
• There are more elegent ways to do things but for this assignment, you can just write the tuples to the file as strings and then read the strings back at the end of the program
• Remember that you'll also need to include a "\n" to each line you write to the file so you don't just end up with one long string on a single line
• Because you are going to be appending to the file each time, it might be good for you to include this as the first thing you do in your main:
o open("student_info.txt","w").close()
o It basically overwrites the file with a blank file.
o This prevents you from appending more and more each time you rerun your program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here there are many ways to solve depending upon the platform like jupyter notebook but i will do simply so lets go for it


def write_to_file(tuple)

f= open("student_info.txt","w") // opening the file

f.write("My name is "+ myname + " \n") // Appending the file

f.close()

def get_student_info():

st=list()

n=int(input('Enter the number of the student :'))

print('Students entry')

for i in range(n):

print('Student :',i+1)

rollno=input('\tRollnumber:')

name=input(\tname:')

age=int(input(\tage:')

st.append(Student(rollno,name,age)

print("Student Information")

print("rollno\t\tname\t\tage")

for i in range(n):

for j in range (i+1,n):

if(st[i].name>st[j].name):

tmp=st[i]

st[i]=st[j]

st[j]=tmp

Add a comment
Answer #2

Program Files:

StudentScores.py

"""
   Python program to read and store student details in a text file
"""

# declaring filename
filename = 'student_info.txt'


def write_to_file(studentTuple):
"""
write the given tuplet to the txt file
"""

# open file to append data
writeFile = open(filename, 'a')

# writing the tuple with a new line character
writeFile.write(studentTuple)
writeFile.write("\n")

# close the file
writeFile.close()


def get_student_info(studentName):
"""
Accepts an argument for a student name
Prompts the user to input as many test scores as they wish
Stores the information (name and scores) in a tuple
"""

# list to store the scores
scores = []

print("Enter Scores for", studentName)

while(True):

score = int(input("Enter score : "))
scores.append(score)

# prompt to user for continue
userChoice = input("Do you want to add more scores (y/n) : ")

# if the user dont want to continue break the while loop
if(userChoice.lower() != "y"):
break

# write the tuplet the text file
write_to_file(str((studentName, scores)))


def read_from_file():
"""
Reads a file line by line
Prints each line to the console
"""

# open the file
readFile = open(filename)

# read lines and print each line
for line in readFile.readlines():
print(line)

# close the file
readFile.close()

# main functon


def main():

# testing 4 students
get_student_info('student1')
get_student_info('student2')
get_student_info('student3')
get_student_info('student4')

# call read_from_file
read_from_file()


if __name__ == '__main__':
main()

student_info.txt

('student1', [23, 45, 55])
('student2', [20])
('student3', [35, 70])
('student4', [100])

output:

Enter Scores for student1
Enter score : 23
Do you want to add more scores (y/n) : y
Enter score : 45
Do you want to add more scores (y/n) : y
Enter score : 55
Do you want to add more scores (y/n) : n
Enter Scores for student2
Enter score : 20
Do you want to add more scores (y/n) : n
Enter Scores for student3
Enter score : 35
Do you want to add more scores (y/n) : y
Enter score : 70
Do you want to add more scores (y/n) : n
Enter Scores for student4
Enter score : 100
Do you want to add more scores (y/n) : n
('student1', [23, 45, 55])

('student2', [20])

('student3', [35, 70])

('student4', [100])

Hope this helps!

Please let me know if any changes needed.

Thank you!

Add a comment
Know the answer?
Add Answer to:
Help needed related python task ! Thanx again How you're doing it • Write a function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • For this task you're going to write a function that joins strings from a list. The function calle...

    I need help with this python programming assignment please double check the indentations For this task you're going to write a function that joins strings from a list. The function called join ) has one formal parameter and one optional parameter. Recall that an optional parameter is like end or sep in the print( function. You don't have to use it because there's a default value. For the print () function, e.g., \'n' (newline) is the default value for end....

  • Help needed in python ! Classes can not be used ! Thanx Many games have complex...

    Help needed in python ! Classes can not be used ! Thanx Many games have complex physics engines, and one major function of these engines is to figure out f two objects are colliding. Weirdly-shaped objects are often approximated as balls. In this problem, we will figure out if two balls are colliding. You will need to remember how to unpack tuples For calculating collision, we only care about a ball's position in space, as well as its size. We...

  • Question 4 Write a function CheckPhrase to check spelling in a phrase It should have the...

    Question 4 Write a function CheckPhrase to check spelling in a phrase It should have the following parameters e a string phrase .a string outputFile e an array of strings correctWords Size of the array correctWords . a 2D array of strings misspelledWordsD12 the number of rows in the misspelledWords array You should do the following with phrase 1. Separate the phrase into words 2. Call CheckWord on each word to check the spelling of the word and construct a...

  • can someone please help me write a python code for this urgently, asap Question: Write a Python function, minmp,...

    can someone please help me write a python code for this urgently, asap Question: Write a Python function, minmp, that is passed a file name and a metal alloy's formula unit structure*". The file will contain metal elements and their properties. The function will return a tuple of the element from the formula with the lowest melting point and that melting point Write a second function, molform, that will be called by the first function, to take the metal alloy's...

  • Python please help! Thanks you Write a code to get an unlimited number of grades from...

    Python please help! Thanks you Write a code to get an unlimited number of grades from the user (the user can press enter to finish the grades input, or use a sentinel, for example-1), and then calculate the GPA of all the grades and displays the GPA To do this you might need some help. Try to follow the following process (and check your progress by printing different values to make sure they are as they supposed to be): 1-...

  • PYTHON 1.Guessing many passwords: Write a function called guess_passwords. It doesn't take any arguments. It should use the function guess, trying out as many possible passwords as possible. Here...

    PYTHON 1.Guessing many passwords: Write a function called guess_passwords. It doesn't take any arguments. It should use the function guess, trying out as many possible passwords as possible. Here are some methods you might try: Try out a hardcoded list of strings that you think people might use as passwords. Try out all words in the word file. Try out all character combinations of length 4 or less (or more if you don't mind waiting). Try out combinations of words...

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • For Python-3 I need help with First creating a text file named "items.txt" that has the...

    For Python-3 I need help with First creating a text file named "items.txt" that has the following data in this order: Potatoes Tomatoes Carrots. Write a python program that 1. Puts this as the first line... import os 2. Creates an empty list 3. Defines main() function that performs the tasks listed below when called. 4. Put these two lines at the top of the main function... if os.path.exists("costlist.txt"): os.remove("costlist.txt") Note: This removes the "costlist.txt" file, if it exists. 5....

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT