Skip to content Skip to sidebar Skip to footer

Importing Csv Files To Python And Subject And Teacher Task Python

My task is to create a Python file that imports data from the CSV file I have created: The python program then must store the CSV columns in a list The program asks the teacher for

Solution 1:

Here is a code which I created with a similar task. Instead of subjects, I used games. Simply change the strings.

import csv

price_list = {} # this is an empty dictionarywithopen('gamesandprices.csv') as csvfile:
readCSV = csv.reader(csvfile)
for row in readCSV:
    price_list[row[0]] = row[1]

what_game = input("Which game(s) would you like to find out the price of?: ")
what_game = what_game.split(' ')

results = [(game, price_list[game]) for game in what_game if game in price_list]

iflen(results) > 6:
print('Please ask prices for a maximum of 6 games')
else:
for game, price in results:
    print('The price of {} is {}'.format(game, price))

Solution 2:

Use for loop to repeat everything 6 times

for x in range(6):
   what_subject = ... 
   subjectdex = ...
   theteacher = ...
   print(...)

Or use split(' ') if you have subjects in one line of text

text = input("subjects (separated by one space): ")

subjects = text.split(' ')

iflen(subjects) > 6:
   print("too much subjects")

for s in subjects:
   subjectdex = ...
   theteacher = ...
   print(...)

EDIT:

import csv

subjects = []
teachers = []

withopen('teachers.csv') as csvfile:
    readCSV = csv.reader(csvfile)

    for row in readCSV:
        subjects.append(row[0])
        teachers.append(row[1])

# ---# for test#subjects = ['a', 'b', 'a', 'c']#teachers = ['X', 'Y', 'Z', 'Q']

what_subject = input("Which subjects did you have today at school? ")

what_subject = what_subject.split(' ')

iflen(what_subject) > 6:
    print("too much subjects")

for one_subject in what_subject:

    # find more then one teacher
    theteacher = []
    subjectdex = 0whileTrue:
        # find next - start at "subjectdex"try:
            subjectdex = subjects.index(one_subject, subjectdex)
        except:
            break# no more subjects - leave loop

        theteacher.append(teachers[subjectdex])

        subjectdex += 1# new start for "index"

    theteacher = ', '.join(theteacher)

    print("The teachers of", one_subject, "are", theteacher)

Post a Comment for "Importing Csv Files To Python And Subject And Teacher Task Python"