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

  • Home
  • SEARCH
  • 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 6805757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:37:31+00:00 2026-05-26T19:37:31+00:00

I’m trying to read from a simple text file that looks like this: 11

  • 0

I’m trying to read from a simple text file that looks like this:

11
eggs
1.17
milk
3.54
bread
1.50
coffee
3.57
sugar
1.07
flour
1.37
apple
.33
cheese
4.43
orange
.37
bananas
.53
potato
.19

What I’m trying to do is ask an input for a file name such as “milk” than to print the price of that. I am trying to use dictionaries.

Here is my code:

def main():
    key = ''
    infile = open('shoppinglist.txt', 'r')

    count = infile.readline()
    groceries = {}

    print('This program keeps a running total of your shopping list.')
    print('Use \'EXIT\' to exit.')

    grocery = input('Enter an item: ')

    for line in infile:
        line = line.strip()
        if key == '':
            key = line
        else:
            groceries[key] = line #maybe use here float(line) instead
            key = ''

    print ('Your current total is $'+ groceries[grocery])

main()

EXPECTED OUTPUT

This program keeps a running total of your shopping list.
Use ‘EXIT’ to exit.
Enter an item: eggs
Your current total is $1.17

Enter an item: bread
Your current total is $3.00

Enter an item: sugar
Your current total is $4.07

Enter an item: EXIT
Your final total is $4.07

  • 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-05-26T19:37:32+00:00Added an answer on May 26, 2026 at 7:37 pm

    Here you have. You already had a pretty ready code.
    Look at the diferences:

    -You were overwritting groceries
    -you were not reading the value from the dictionary
    -In python < 3x you shoud use raw_input instead of input. If you are in py3k input is OK
    -Also you do not need parenthesis for print in py2.x. You do in py3k
    -Maybe the only bizarre thing for a homework here is the use of % in print. It means that %s will be substituted by the string after the last % sign
    -Be careful because cost of groceries are actually strings so you can not make mathematical operations with them. First you should convert them to floats.

    def main():
        key = ''
        infile = open('shoppinglist.txt', 'r')
    
        count = infile.readline()
        groceries = {}
    
        print('This program keeps a running total of your shopping list.')
        print('Use \'EXIT\' to exit.')
    
        grocery = raw_input('Enter an item: ')
    
        for line in infile:
            line = line.strip()     #take out newlines codes.
            if key == '':
                key = line
            else:
                groceries[key] = line     #maybe use here float(line) instead
                key = ''
    
        print 'Your current total is %s $' % groceries[grocery]
    
    main()
    

    For several inputs use (not tested, py3k code, remember to convert costs to floats before):

    total = 0
    while True:
      grocery = input('Enter an item: ')
      if grocery == 'EXIT':
           print('Your final total is $%s' %total)
           break
      else:
           cost = groceries[grocery]
           total += cost
           print('Your current total is $%s' %total)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.