Question

In: Computer Science

For Python, I cannot figure out why "and" is printing at the beginning of my list....

For Python, I cannot figure out why "and" is printing at the beginning of my list. Can someone help?

listToPrint = []

while True:

newWord = input("Enter a word to add to the list (prest return to stop adding words) > ")

newWord == "":

break

else:

listToPrint.append(newWord)

for item in listToPrint[:-1]:

print(item, end=', ')

print('and', listToPrint[-1])

If I enter m, n, b to the list, it executes to

and m, n, b

Solutions

Expert Solution

Probably it is due to indentation issue in your for loop:-

Here is a code working properly -> Make sure to use proper indentation

Code is Given Below:

====================

listToPrint = []
while True:
newWord = input("Enter a word to add to the list (prest return to stop adding words) > ")
if newWord == "":
break
else:
listToPrint.append(newWord)

for item in listToPrint[:-1]:
print(item, end=', ')

print('and', listToPrint[-1])

Output:

=============

Code Snapshot:

===================


Related Solutions

I cannot figure out why my random name generator isn't producing the letter u or z....
I cannot figure out why my random name generator isn't producing the letter u or z. Can someone help me out? The language is java. import java.util.Random; public class BrandName {    public static void main(String[] args) {        Random random = new Random();        System.out.println("Brand Name Generator - Michael Wikstrom\n");        char odd[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y',       ...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user decide the size of a 2d array then fill said array. #include <iostream> using namespace std; typedef int* IntArrayPtr; int main(void) { int column, row, i, j; IntArrayPtr *arr_num = new IntArrayPtr[row]; cout << " " << endl << " This program creates a 2D array of dynamic memory, fills it with user entered numbers, " << endl << " Then prints the array...
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
Cant figure out why my out is wrong for my c++ program. for example if the...
Cant figure out why my out is wrong for my c++ program. for example if the initial value is 100, the intrest rate is 10%, and it takes 6 months for maturity the output should be $105 but instead it outputs 101.657 please help. #include <iostream> #include <cmath> using namespace std; struct account { double balance; double interest_rate; int term; }; void info(account& accountinfo); int main(void) { double calc1, calc2, calc3; account accountinfo; info(accountinfo); calc1 = accountinfo.interest_rate / 100; calc2...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
Show work please, this is the last problem for my homework and I cannot figure this...
Show work please, this is the last problem for my homework and I cannot figure this out for the life of me An IP4 datagram arrived with the following information in the header (in hexadecimal):                0x4A 00 00 56 00 03 58 50 18 06 58 50 7C 4E 03 02 B4 0E OF 15 2f………. Are there any options? How many bytes, if any? How many more routers can the packet travel to? Explain. What is the size...
I have done the first part of this problem, but I cannot figure out the second...
I have done the first part of this problem, but I cannot figure out the second part. I know there are several different answers already posted on Chegg for this particular problem with different acceleration and time, but I need this one specifically, with a step by step solution. Again...just PART B....the answer to Part A is 201.72 A helicopter carrying Dr. Evil takes off with a constant upward acceleration of 4.20 m/s2. Secret agent Austin Powers jumps on just...
this is my assignment for intro to java tha i could not figure out, my system...
this is my assignment for intro to java tha i could not figure out, my system just crashed wit a lot of errors. thank for your help Create a new Java class inside your project folder. The name of the class should be: TempConverter Note that this means you should have the following line at the top of your program: public class TempConverter Write a program that allows the user to convert a temperature given in degrees Celsius or Fahrenheit...
I cannot for the life of me figure out the definitions of the correspondence, coherence, pragmatic...
I cannot for the life of me figure out the definitions of the correspondence, coherence, pragmatic and semantic theories of truth. I've tried deciphering the definitions from my textbook but I still can't wrap my head around it. I need someone to dumb it down for me. Thank you in advance.
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT