Question

PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a...

PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :)

First create a text file named "items.txt" that has the following data in this order:

Potatoes
Tomatoes
Carrots

... and have it be in the same directory as the Python program you are coding for this assignment.


IMPORTANT: Your program should work with any size file. It should work with 100 items listed or with no items in the file!



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. Display "Assignment 6"

6. Prompts the user to enter a file name.

a. If the file name entered is "end" then end the program.

b. If the file name is "items.txt", open the file for reading,
replace all the newline characters with an empty string, and
add all of the items from the file to the list that was
defined in step #2

c. If a FileNotFoundError exception occurs, display a message
that lets the user know the file was not found and gives
them a chance to try it again.

7. Sort the list.

8. For each element in the list, prompt the user to enter the price
for the element, e.g. How much should Carrots cost?

Convert the number entered into a float.

a. If the number entered does not convert properly into a float,
and gets a ValueError exception, then display a couple error
messages in the following format...

You entered an invalid float that could not convert string to float: 'ss'
Skipping to the next item after (element)

... and then prompt the user to enter the price of the next element
from the list.

b. If the number does convert properly to a float then create a string
variable with the following format...

(element) have a cost of (cost) dollars

... and open the "costlist.txt" file for appending and write the string
created to a line in the file.


9. Display "Cost List"

10. Display each line from the "costlist.txt" file

11. Display "Program End"

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

ANSWER :

CODE :

import os

list1 = []


def main():
    try:
        if os.path.exists("costlist.txt"):
            os.remove("costlist.txt")
        print("Assignment 6")

        file1 = input("Enter a file name : ")
        if file1.lower() == 'end':
            exit()
        elif file1.lower() == 'items.txt':
            fo = open(file1, 'r')
            data = fo.readlines()
            for i in data:
                i = i.replace("\n", '')
                list1.append(i)
    except FileNotFoundError:
        print("file was not found.Try again...!")
        main()

    sorted_list1 = sorted(list1)

    file2 = open("costlist.txt", 'w')
    for i in sorted_list1:
        cost = input("How much should " + i + " cost?")
        try:
            cost = float(cost)
            str1 = i + " have a cost of " + str(cost) + " dollars"
            file2.write(str1+"\n")
        except ValueError:
            print("You entered an invalid float that could not convert string to float : ", cost)
    file2.close()
    file_open = open("costlist.txt", 'r')
    cost_file_data = file_open.readlines()
    print("Cost List")
    for i in cost_file_data:
        print(i)
    print("Program End")


if __name__ == '__main__':
    main()

OUTPUT :

THANK YOU....!!!

Add a comment
Know the answer?
Add Answer to:
PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a...
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
  • 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....

  • I need help building code in python for this: Create a program that: Creates a sales...

    I need help building code in python for this: Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file...

  • This question is for Python 3. I can get the first couple of steps in this...

    This question is for Python 3. I can get the first couple of steps in this question but I get errors. Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save...

  • Create a Python list (use first three letters of your name followed by the letter L...

    Create a Python list (use first three letters of your name followed by the letter L as the name of the list.) with the following data: 10, 20.0, 50.00, 7000, 7500, 7700, 7800, 8000, 9000, ‘python’. Display the entire list with an appropriate message. Display the 4th element in the list with an appropriate message. Display the first 3 values of the list only. Display all the values starting from the sixth element to the end of the list. Change...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for...

    PYTHON CODING Create a program that prompts the user twice. The first prompt should ask for the user's most challenging course at Wilmington University. The second prompt should ask for the user's second most challenging course. The red boxes indicate the input from the user. Your program should respond with two copies of an enthusiastic comment about both courses. Be sure to include the input provided by the user to display the message. There are many ways to display a...

  • I need help with this python programming exercise, please! thanks in advance Create a Python script...

    I need help with this python programming exercise, please! thanks in advance Create a Python script file called hw4.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Ex. 1. Write a program that inputs an integer number from the user, then prints a letter "O" in ASCII art using a width of 5 and the...

  • Python Coding Help

    How to write a code for this?When the program starts the bowler is prompted for a score for ‘Game 1’Accept the bowling score from the user; valid bowling scores are whole numbers between 0 and 300.Once a valid numeric score is entered then perform range validation to ensure it is within the required entry range. If the bowler enters a value outside of the acceptable range, or for any entries that aren’t whole numbers, the program should display an appropriate...

  • for python-3 I want to prompt the user to enter their first name and then Call...

    for python-3 I want to prompt the user to enter their first name and then Call the is_field_blank function to see if nothing was entered.and If nothing was entered i want it to display the following message "First Name must be Entered" and then re-prompt the user to enter the first name. I want it to do this repeatedly until the user enters a first name. this is what i have so far def main():     yourFirst = input("What is...

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

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