aero26
aero26

Reputation: 155

What would be the missing step in the accumulator for this program?

So I've been assigned to write a program in python that prompts the user to type a word, then deletes letters from the word in sequence. The first letter in the first line, the second in the second, etc.

For example, for "Atlanta":

tlanta
alanta
atanta
atlnta
atlata
atlana
atlant

This is what I've got and semantically, it's not doing anywhere close to what I want.! I guess it's clear the problem lies in the first and second lines of the for loop. I'm not sure what to do.

Upvotes: 0

Views: 50

Answers (2)

chrk
chrk

Reputation: 4215

You can think of it like this:

You need to loop through the process of printing the whole word except from a letter, as many times as the number of letters that consist the word.

So it would go like this:

word = raw_input("Enter word:")

for i in range(len(word)):      # loop as many times as the number of letters
    print word[:i] + word[i+1:] # print all letters but the current iteration's

Upvotes: 0

abarnert
abarnert

Reputation: 365707

The problem is this:

end = word - letter

Here, word is a string, like atlanta, and letter is a number, like 2. What do you expect subtracting them to do?

What you want to do here is get all of the letters of atlanta except #2, right? You do that by slicing. With those values, word[:letter] will get you 'at', while word[letter:] will get you 'lanta'. You should be able to figure out from that how to get what you actually want, 'anta'.

And once you have 'at' and 'anta', all you have to add is concatenate them, with +, and print the result.

Upvotes: 1

Related Questions