Python Forum
New to Python - Not sure why this code isn't working - Any help appreciated
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python - Not sure why this code isn't working - Any help appreciated
#1
Hi there! New to this forum and Python (Technically my first language I am learning to program in! :D) and I'm running into an issue that I don't quite understand.
So I've been watching youtuber video called Harvard CS50’s Introduction to Programming with Python – Full University Course on the channel freecodeCamp.org. During lecture at 8:09:01 we re-code what the previous example that he was explaining to make it easier to catch multiple columns in a CSV doc if it so happens to have any or some other format type. In the example we are using few Harry Potter Characters. Harry, Ron and Draco. End goal of this example is to pull data from a CSV doc that written like the following
Harry, "Number Four, Privent Drive"
Ron, The Burrow
Draco, Malfoy Manor

And to solve this what he suggest was us to import the library csv and use csv.reader to help us out with this to get the desire end result for the program to print: Harry is from Number Four, Privent Drive
Ron is from The Burrow
Draco is from Malfoy Manor.


And code we have written in Python
import csv

students = []

with open("harrypotter.csv") as file:
    reader = csv.reader(file)
    for name, home in reader:
        students.append({"name": name, "home": home})

for student in sorted(students, key=lambda student: student["name"]):
    print(f"{student['name']} is from {student['home']}")
When lecturer in the video runs this line of code he gets the result we are looking for, But when I run it I get following Error Message:
Error:
line 7, in <module> for name, home in reader: ^^^^^^^^^^ ValueError: too many values to unpack (expected 2)
And I am not sure why. I've looked and read the code multiple times, I've looked at what the lecturer wrote and compared and I just don't understand Why mines is failing and yet when he ran his it passed and worked.

Any ideas?


The link to the video I am watching is
https://www.youtube.com/watch?v=nLRL_NcnK-4&t=29112s

Time Stamps:
Example starts at 8:00:05
Running the code I wrote above is at 8:04:30
deanhystad write Jul-21-2023, 04:14 PM:
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.
Reply


Messages In This Thread
New to Python - Not sure why this code isn't working - Any help appreciated - by TheGreatNinx - Jul-21-2023, 03:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Any help is appreciated! Butch12 3 876 Jul-22-2023, 03:23 AM
Last Post: Larz60+
  code not working when executed from flask app ThomasDC 1 1,118 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 2,077 May-09-2023, 08:47 AM
Last Post: buran
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,158 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,226 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 2,119 Mar-21-2022, 10:12 AM
Last Post: End3r
  interloper help appreciated combining two python3 scripts to add new conditions. Ricktoddfrank 0 1,546 Jun-22-2021, 10:24 PM
Last Post: Ricktoddfrank
  I don't undestand why my code isn't working. RuyCab 2 2,090 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 3,094 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,965 Feb-09-2021, 07:49 PM
Last Post: Patricia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020