Python Forum
Need help with an assignment, not an answer.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with an assignment, not an answer.
#1
So I was given code to essentially begin the assignment, but for some reason I cannot for the life of me figure out where to go with it. I tried to do it on my own without the given code, but it went nowhere.
Essentially the idea is a user has to input words, and the code creates a list, simple, but in this assignment, I have to get the list to add 'and' with a space to the last word. This is where I am getting stuck, but the code I was given to begin makes me even more confused.

listToPrint = []
while True:
    newWord = input("Enter a word to add to the list (press return to stop adding words) > ")
    if newWord == "":
        break
    else:
        listToPrint.append(newWord)
It literally is just putting a word in, and starting the input over again. It doesn't compile a list, it doesn't really do much of anything except repeat. I just don't understand where to start with this, should I just scrap it and start my own code for this?
Reply
#2
You just need one line before the break line. On that line you want to insert 'and' between the last item and the next to last item in the list. There is an insert method of lists you can use to do this.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-01-2019, 01:12 AM)ichabod801 Wrote: You just need one line before the break line. On that line you want to insert 'and' between the last item and the next to last item in the list. There is an insert method of lists you can use to do this.

So I took your information and I think I may have gotten the code before the break, but it still doesn't print a list, it just keeps revolving back to "Enter a word to add to the list" and won't print anything.

listToPrint = []
print(listToPrint)
while True:
    newWord = input("Enter a word to add to the list (press return to stop adding words) > ")
    if newWord == "":
        newWord.insert (', and') + [-2]
        break
    else:
        listToPrint.append(newWord)
I don't know why I am struggling with this so much, the other problem was simple, this just seems to be confusing me more than anything.
Reply
#4
I think I misunderstood the assignment a bit. I was thinking given the user entering 'spam' three times, the ['spam', 'spam', 'and', 'spam']. But I guess the goal is actually ['spam', 'spam', 'and spam'].

In that case you want to do three things when newWord is empty: add 'and ' to the start of newWord, append newWord to listToPrint, and break.

If you want the list to print at the end, you need to move (or copy) line 2 to the end of the program.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Couple of things -
You say it does not compile a list. It does. The listToPrint.append(newWord) appends the entered words to the list building it along.
It doesn't print anything because you don't tell it to print anything. Once you have listToPrint complete (the user entered a blank line), you then need to print(listToPrint).
The challenge is then as ichabod801 said. When the user enters that blank line you want to break to your print statement, but before you print you need to modify the last item in the list (until the person enters the blank line you never know if the item entered will be the last).

So, take your original code and add
listToPrint[-1]="and "+listToPrint[-1]
print(listToPrint)
to the end, unindented. listToPrint[-1] is the last item. You will append "and " to the front of that and store it back into that last item. Then print it to see the result.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Answer for newbie, Sqlite3 Froger 2 812 Sep-27-2023, 05:33 PM
Last Post: noisefloor
  Coding answer for newbie cg3 2 1,111 Aug-05-2023, 12:17 PM
Last Post: jefsummers
  What am I doing wrong to not get the answer pav1983 7 3,634 Jun-25-2020, 08:53 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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