Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:58:22+00:00 2026-06-03T15:58:22+00:00

I have an assessment to do, and here’s my code so far: number1 =

  • 0

I have an assessment to do, and here’s my code so far:

number1 = input("Number1? ")

number2 = input("Number2? ")

packages = csv.reader(open('lol.txt', newline='\n'), delimiter=',')
for PackName,PriceAdultString,PriceChildString in packages:
    n += 1
    PriceAdult = float(PriceAdultString)
    PriceChild = float(PriceChildString)
    print("%i. %17s - $%4.2d / $%4.2d" % (n, PackName, PriceAdult, PriceChild))

NameChoice = input("Which name would you like? Choose using a number: ")

The lol.txt used by csv.reader consists of the following:

herp,123,456    
derp,321,654    
hurr,213,546

Now, I need to be able to use NameChoice to retrieve a row from the file, and use the data within as name, number1, and number2, so for NameChoice == 1, name = herp, number1 = 123 and number 2 = 456, and the numbers must be a floating point number.

I’m having trouble figuring this out, and could use some guidance if that’s possible.

Thanks all.

Before it’s asked, I realised I forgot to mention: I have googled, and trawled through the Python guides, and my textbooks. I’m not entirely convinced I know what I’m looking for, though.

Run into a new problem:
I need to be able to take CSV text with ‘\n\n’ instead of ‘\n’, so the text is more like the following:

herp,123,456

derp,321,654

hurr,213,546

My (very slightly adjusted) version of the code Li-Aung used:

import csv
with open ("lol.txt",'rt', newline = '\n\n') as f:
    csv_r = csv.reader (f)
    entries = [ (name, float(p1), float(p2)) for name, p1, p2 in csv_r]

for index, entry in enumerate(entries):
    print ("%2i. %-10s %5.2f %5.2f" % (index, entry[0], entry[1], entry[2]))

choice = int(input("Choose a number: "))

print (entries[choice])

Which returns the exception:

Traceback (most recent call last):
  File "C:/Python32/eh", line 2, in <module>
    with open ("lol.txt",'rt', newline = '\n\n') as f:
ValueError: illegal newline value: 

Now, the debug is pretty clear – ‘\n\n’ is not acceptable as a newline specifier, but I was wondering if there is a way around this?

Note: Screwed up the previous edit, the debug from the code with ” newline = ‘\n'” would have been:

Traceback (most recent call last):
  File "C:/Python32/eh", line 4, in <module>
    entries = [ (name, float(p1), float(p2)) for name, p1, p2 in csv_r]
  File "C:/Python32/eh", line 4, in <listcomp>
    entries = [ (name, float(p1), float(p2)) for name, p1, p2 in csv_r]
ValueError: need more than 0 values to unpack

Which is because it treated the blank space with 0 values between each useful row as a row, as it was told to do, and there was nothing in there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:58:26+00:00Added an answer on June 3, 2026 at 3:58 pm

    Store the entire file to a list:

    import csv
    with open ("lol.txt",'rb') as f:
        csv_r = csv.reader (f)
        entries = [ (name, float(p1), float(p2)) for name, p1, p2 in csv_r]
    
    for index, entry in enumerate(entries):
        print ("%2i. %-10s %5.2f %5.2f" % (index, entry[0], entry[1], entry[2]))
    
    choice = int(raw_input("Choose a number: "))
    
    print (entries[choice])
    

    Output:

     0. herp       123.00 456.00
     1. derp       321.00 654.00
     2. hurr       213.00 546.00
    Choose a number: 0
    ('herp', 123.0, 456.0)
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a table that is used for assessment-values in our tool where each
So I have the following XML chain: $xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue XML example In Some XML-files $xml->assessment->outcomes_processing->outcomes->...
Have you ever obfuscated your code before? Are there ever legitimate reasons to do
Have you ever created or encountered a self modifying code in Java? If yes,
So here I believe I have a small buffer overflow problem I found when
I have XML like this: <assessment name=Assessment> <section name=Section1> <item name=Item1-1/> <item name=Item1-2/> <item
I have XML like this: <assessment> <variables> <variable> <attributes> <variable_name value=FRED/> </attributes> </variable> </variables>
I have the following XML: <assessment> <section> <item name=Item1/> <item name=Item2/> <item name=Item3> <attributes>
I have the following XML: <assessment> <section> <item> <attributes> <variables> <variable> <variable_name value=MORTIMER/> </variable>
Suppose I have XML like this: <xsl:template match=assessment | section > . . .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.