Python Replace Blanks With Letter










0















I want the letters the user types (granted that they're in the 'letters') to replace the blanks in their correct sequential position (I don't want 'agbdf__'), and stop when all the letters are typed in. As the code is now, it requires letters to be typed multiple times, and it stops if the letter 'g' is typed seven times. This is a part of a hangman code I'm trying to implement. If anyone could post the right way to program this (not just a tip, because I most likely won't be able to figure out how to implement it), it would be much appreciated.



letters='abcdefg' 
blanks='_'*len(letters)
print('type letters from a to g')
print(blanks)
for i in range(len(letters)):
if letters[i] in input():
blanks = blanks[:i] + letters[i] + blanks[i+1:]
print(blanks)









share|improve this question



















  • 1





    Just because your other question was closed doesn't mean you can repeat it and create a duplicate.

    – Burhan Khalid
    Sep 3 '13 at 4:41











  • You didn't provide any explanation with the answer you gave me, and the people who responded in that question were very impolite. So pardon me if I ask it again and get answers that actually help me, Burhan.

    – user2734880
    Sep 3 '13 at 23:00
















0















I want the letters the user types (granted that they're in the 'letters') to replace the blanks in their correct sequential position (I don't want 'agbdf__'), and stop when all the letters are typed in. As the code is now, it requires letters to be typed multiple times, and it stops if the letter 'g' is typed seven times. This is a part of a hangman code I'm trying to implement. If anyone could post the right way to program this (not just a tip, because I most likely won't be able to figure out how to implement it), it would be much appreciated.



letters='abcdefg' 
blanks='_'*len(letters)
print('type letters from a to g')
print(blanks)
for i in range(len(letters)):
if letters[i] in input():
blanks = blanks[:i] + letters[i] + blanks[i+1:]
print(blanks)









share|improve this question



















  • 1





    Just because your other question was closed doesn't mean you can repeat it and create a duplicate.

    – Burhan Khalid
    Sep 3 '13 at 4:41











  • You didn't provide any explanation with the answer you gave me, and the people who responded in that question were very impolite. So pardon me if I ask it again and get answers that actually help me, Burhan.

    – user2734880
    Sep 3 '13 at 23:00














0












0








0


1






I want the letters the user types (granted that they're in the 'letters') to replace the blanks in their correct sequential position (I don't want 'agbdf__'), and stop when all the letters are typed in. As the code is now, it requires letters to be typed multiple times, and it stops if the letter 'g' is typed seven times. This is a part of a hangman code I'm trying to implement. If anyone could post the right way to program this (not just a tip, because I most likely won't be able to figure out how to implement it), it would be much appreciated.



letters='abcdefg' 
blanks='_'*len(letters)
print('type letters from a to g')
print(blanks)
for i in range(len(letters)):
if letters[i] in input():
blanks = blanks[:i] + letters[i] + blanks[i+1:]
print(blanks)









share|improve this question
















I want the letters the user types (granted that they're in the 'letters') to replace the blanks in their correct sequential position (I don't want 'agbdf__'), and stop when all the letters are typed in. As the code is now, it requires letters to be typed multiple times, and it stops if the letter 'g' is typed seven times. This is a part of a hangman code I'm trying to implement. If anyone could post the right way to program this (not just a tip, because I most likely won't be able to figure out how to implement it), it would be much appreciated.



letters='abcdefg' 
blanks='_'*len(letters)
print('type letters from a to g')
print(blanks)
for i in range(len(letters)):
if letters[i] in input():
blanks = blanks[:i] + letters[i] + blanks[i+1:]
print(blanks)






python replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 3 '13 at 4:30









0xc0de

4,63623563




4,63623563










asked Sep 3 '13 at 4:06









user2734880user2734880

33




33







  • 1





    Just because your other question was closed doesn't mean you can repeat it and create a duplicate.

    – Burhan Khalid
    Sep 3 '13 at 4:41











  • You didn't provide any explanation with the answer you gave me, and the people who responded in that question were very impolite. So pardon me if I ask it again and get answers that actually help me, Burhan.

    – user2734880
    Sep 3 '13 at 23:00













  • 1





    Just because your other question was closed doesn't mean you can repeat it and create a duplicate.

    – Burhan Khalid
    Sep 3 '13 at 4:41











  • You didn't provide any explanation with the answer you gave me, and the people who responded in that question were very impolite. So pardon me if I ask it again and get answers that actually help me, Burhan.

    – user2734880
    Sep 3 '13 at 23:00








1




1





Just because your other question was closed doesn't mean you can repeat it and create a duplicate.

– Burhan Khalid
Sep 3 '13 at 4:41





Just because your other question was closed doesn't mean you can repeat it and create a duplicate.

– Burhan Khalid
Sep 3 '13 at 4:41













You didn't provide any explanation with the answer you gave me, and the people who responded in that question were very impolite. So pardon me if I ask it again and get answers that actually help me, Burhan.

– user2734880
Sep 3 '13 at 23:00






You didn't provide any explanation with the answer you gave me, and the people who responded in that question were very impolite. So pardon me if I ask it again and get answers that actually help me, Burhan.

– user2734880
Sep 3 '13 at 23:00













3 Answers
3






active

oldest

votes


















1














Change your loop to:



for i in range(len(letters)):
letter = raw_input()
index = letters.index(letter)
blanks = blanks[:index] + letter + blanks[index + 1:]
print blanks


You are not replacing the correct blank/underscore, you are just sequentially replacing them. Instead you need to find the correct blank to replace, and then replace that. Also don't expect this code to be running infinitely until the blanks is completely filled. If you want that, you can figure that out I guess (as your question is specific to replacing correct letters). Also you might want this program should handle inputs other than 'abcdef' too :).






share|improve this answer

























  • Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

    – user2734880
    Sep 3 '13 at 8:48












  • the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

    – TehTris
    Sep 3 '13 at 17:23











  • It apparently isn't, but thanks for your input.

    – user2734880
    Sep 3 '13 at 21:49











  • I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

    – user2734880
    Sep 4 '13 at 9:04












  • @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

    – 0xc0de
    Sep 4 '13 at 9:53


















0














letters = 'abcdefg' 

print('type letters from a to g')

all_letters = list(letters)
blanks = ['_'] * len(letters)

while True:
guessed_letter = input("guess > ")

while guessed_letter in all_letters:
index = all_letters.index(guessed_letter)
all_letters[index] = None
blanks[index] = guessed_letter

output = ''.join(blanks)
print(output)

if letters == output:
print("gg")
break


Or if you prefere a more stringy version



letters = 'abcdefg' 

print('type letters from a to g')

all_letters = list(letters)
blanks = '_' * len(letters)

while True:
guessed_letter = input("guess > ")

while guessed_letter in all_letters:
index = all_letters.index(guessed_letter)
all_letters[index] = None
blanks = blanks[:index] + guessed_letter + blanks[index+1:]

print(blanks)

if letters == blanks:
print("gg")
break





share|improve this answer
































    0














    You could use a list comprehension for this. Here's an example:



    letters = 'abcdefg'
    typed = raw_input('Type the characters a through g: ')
    print ''.join(s if s in typed else '_' for s in letters)


    Sample Output:



    Type the characters a through g: abdef
    ab_def_


    Here's a more complete/advanced hangman example: https://gist.github.com/dbowring/6419866






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18583929%2fpython-replace-blanks-with-letter%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Change your loop to:



      for i in range(len(letters)):
      letter = raw_input()
      index = letters.index(letter)
      blanks = blanks[:index] + letter + blanks[index + 1:]
      print blanks


      You are not replacing the correct blank/underscore, you are just sequentially replacing them. Instead you need to find the correct blank to replace, and then replace that. Also don't expect this code to be running infinitely until the blanks is completely filled. If you want that, you can figure that out I guess (as your question is specific to replacing correct letters). Also you might want this program should handle inputs other than 'abcdef' too :).






      share|improve this answer

























      • Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

        – user2734880
        Sep 3 '13 at 8:48












      • the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

        – TehTris
        Sep 3 '13 at 17:23











      • It apparently isn't, but thanks for your input.

        – user2734880
        Sep 3 '13 at 21:49











      • I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

        – user2734880
        Sep 4 '13 at 9:04












      • @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

        – 0xc0de
        Sep 4 '13 at 9:53















      1














      Change your loop to:



      for i in range(len(letters)):
      letter = raw_input()
      index = letters.index(letter)
      blanks = blanks[:index] + letter + blanks[index + 1:]
      print blanks


      You are not replacing the correct blank/underscore, you are just sequentially replacing them. Instead you need to find the correct blank to replace, and then replace that. Also don't expect this code to be running infinitely until the blanks is completely filled. If you want that, you can figure that out I guess (as your question is specific to replacing correct letters). Also you might want this program should handle inputs other than 'abcdef' too :).






      share|improve this answer

























      • Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

        – user2734880
        Sep 3 '13 at 8:48












      • the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

        – TehTris
        Sep 3 '13 at 17:23











      • It apparently isn't, but thanks for your input.

        – user2734880
        Sep 3 '13 at 21:49











      • I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

        – user2734880
        Sep 4 '13 at 9:04












      • @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

        – 0xc0de
        Sep 4 '13 at 9:53













      1












      1








      1







      Change your loop to:



      for i in range(len(letters)):
      letter = raw_input()
      index = letters.index(letter)
      blanks = blanks[:index] + letter + blanks[index + 1:]
      print blanks


      You are not replacing the correct blank/underscore, you are just sequentially replacing them. Instead you need to find the correct blank to replace, and then replace that. Also don't expect this code to be running infinitely until the blanks is completely filled. If you want that, you can figure that out I guess (as your question is specific to replacing correct letters). Also you might want this program should handle inputs other than 'abcdef' too :).






      share|improve this answer















      Change your loop to:



      for i in range(len(letters)):
      letter = raw_input()
      index = letters.index(letter)
      blanks = blanks[:index] + letter + blanks[index + 1:]
      print blanks


      You are not replacing the correct blank/underscore, you are just sequentially replacing them. Instead you need to find the correct blank to replace, and then replace that. Also don't expect this code to be running infinitely until the blanks is completely filled. If you want that, you can figure that out I guess (as your question is specific to replacing correct letters). Also you might want this program should handle inputs other than 'abcdef' too :).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Sep 3 '13 at 4:32

























      answered Sep 3 '13 at 4:25









      0xc0de0xc0de

      4,63623563




      4,63623563












      • Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

        – user2734880
        Sep 3 '13 at 8:48












      • the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

        – TehTris
        Sep 3 '13 at 17:23











      • It apparently isn't, but thanks for your input.

        – user2734880
        Sep 3 '13 at 21:49











      • I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

        – user2734880
        Sep 4 '13 at 9:04












      • @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

        – 0xc0de
        Sep 4 '13 at 9:53

















      • Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

        – user2734880
        Sep 3 '13 at 8:48












      • the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

        – TehTris
        Sep 3 '13 at 17:23











      • It apparently isn't, but thanks for your input.

        – user2734880
        Sep 3 '13 at 21:49











      • I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

        – user2734880
        Sep 4 '13 at 9:04












      • @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

        – 0xc0de
        Sep 4 '13 at 9:53
















      Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

      – user2734880
      Sep 3 '13 at 8:48






      Thanks, this is what I've been trying to learn to do for a while. Could you tell me what it's called when you link variables with the '.' so I can look it up? Or, if you have the time to tell me what exactly the '.' is doing in this code , could you?

      – user2734880
      Sep 3 '13 at 8:48














      the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

      – TehTris
      Sep 3 '13 at 17:23





      the . is not linking variables. its calling a variable/method that belongs to the object. You should maybe visit some python tutorial websites, as this is day one programming knowledge.

      – TehTris
      Sep 3 '13 at 17:23













      It apparently isn't, but thanks for your input.

      – user2734880
      Sep 3 '13 at 21:49





      It apparently isn't, but thanks for your input.

      – user2734880
      Sep 3 '13 at 21:49













      I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

      – user2734880
      Sep 4 '13 at 9:04






      I can't figure out how to get the program to keep running if more than 7 letters are typed. I know its probably easy but I can't seem to get it. Anyone mind helping me with this part?

      – user2734880
      Sep 4 '13 at 9:04














      @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

      – 0xc0de
      Sep 4 '13 at 9:53





      @user2734880, We are looping through the letters so if you want to forgive say 3 chances, you can: for i in range(len(letters) + 3):.

      – 0xc0de
      Sep 4 '13 at 9:53













      0














      letters = 'abcdefg' 

      print('type letters from a to g')

      all_letters = list(letters)
      blanks = ['_'] * len(letters)

      while True:
      guessed_letter = input("guess > ")

      while guessed_letter in all_letters:
      index = all_letters.index(guessed_letter)
      all_letters[index] = None
      blanks[index] = guessed_letter

      output = ''.join(blanks)
      print(output)

      if letters == output:
      print("gg")
      break


      Or if you prefere a more stringy version



      letters = 'abcdefg' 

      print('type letters from a to g')

      all_letters = list(letters)
      blanks = '_' * len(letters)

      while True:
      guessed_letter = input("guess > ")

      while guessed_letter in all_letters:
      index = all_letters.index(guessed_letter)
      all_letters[index] = None
      blanks = blanks[:index] + guessed_letter + blanks[index+1:]

      print(blanks)

      if letters == blanks:
      print("gg")
      break





      share|improve this answer





























        0














        letters = 'abcdefg' 

        print('type letters from a to g')

        all_letters = list(letters)
        blanks = ['_'] * len(letters)

        while True:
        guessed_letter = input("guess > ")

        while guessed_letter in all_letters:
        index = all_letters.index(guessed_letter)
        all_letters[index] = None
        blanks[index] = guessed_letter

        output = ''.join(blanks)
        print(output)

        if letters == output:
        print("gg")
        break


        Or if you prefere a more stringy version



        letters = 'abcdefg' 

        print('type letters from a to g')

        all_letters = list(letters)
        blanks = '_' * len(letters)

        while True:
        guessed_letter = input("guess > ")

        while guessed_letter in all_letters:
        index = all_letters.index(guessed_letter)
        all_letters[index] = None
        blanks = blanks[:index] + guessed_letter + blanks[index+1:]

        print(blanks)

        if letters == blanks:
        print("gg")
        break





        share|improve this answer



























          0












          0








          0







          letters = 'abcdefg' 

          print('type letters from a to g')

          all_letters = list(letters)
          blanks = ['_'] * len(letters)

          while True:
          guessed_letter = input("guess > ")

          while guessed_letter in all_letters:
          index = all_letters.index(guessed_letter)
          all_letters[index] = None
          blanks[index] = guessed_letter

          output = ''.join(blanks)
          print(output)

          if letters == output:
          print("gg")
          break


          Or if you prefere a more stringy version



          letters = 'abcdefg' 

          print('type letters from a to g')

          all_letters = list(letters)
          blanks = '_' * len(letters)

          while True:
          guessed_letter = input("guess > ")

          while guessed_letter in all_letters:
          index = all_letters.index(guessed_letter)
          all_letters[index] = None
          blanks = blanks[:index] + guessed_letter + blanks[index+1:]

          print(blanks)

          if letters == blanks:
          print("gg")
          break





          share|improve this answer















          letters = 'abcdefg' 

          print('type letters from a to g')

          all_letters = list(letters)
          blanks = ['_'] * len(letters)

          while True:
          guessed_letter = input("guess > ")

          while guessed_letter in all_letters:
          index = all_letters.index(guessed_letter)
          all_letters[index] = None
          blanks[index] = guessed_letter

          output = ''.join(blanks)
          print(output)

          if letters == output:
          print("gg")
          break


          Or if you prefere a more stringy version



          letters = 'abcdefg' 

          print('type letters from a to g')

          all_letters = list(letters)
          blanks = '_' * len(letters)

          while True:
          guessed_letter = input("guess > ")

          while guessed_letter in all_letters:
          index = all_letters.index(guessed_letter)
          all_letters[index] = None
          blanks = blanks[:index] + guessed_letter + blanks[index+1:]

          print(blanks)

          if letters == blanks:
          print("gg")
          break






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 3 '13 at 4:56

























          answered Sep 3 '13 at 4:42









          XevelionXevelion

          79959




          79959





















              0














              You could use a list comprehension for this. Here's an example:



              letters = 'abcdefg'
              typed = raw_input('Type the characters a through g: ')
              print ''.join(s if s in typed else '_' for s in letters)


              Sample Output:



              Type the characters a through g: abdef
              ab_def_


              Here's a more complete/advanced hangman example: https://gist.github.com/dbowring/6419866






              share|improve this answer





























                0














                You could use a list comprehension for this. Here's an example:



                letters = 'abcdefg'
                typed = raw_input('Type the characters a through g: ')
                print ''.join(s if s in typed else '_' for s in letters)


                Sample Output:



                Type the characters a through g: abdef
                ab_def_


                Here's a more complete/advanced hangman example: https://gist.github.com/dbowring/6419866






                share|improve this answer



























                  0












                  0








                  0







                  You could use a list comprehension for this. Here's an example:



                  letters = 'abcdefg'
                  typed = raw_input('Type the characters a through g: ')
                  print ''.join(s if s in typed else '_' for s in letters)


                  Sample Output:



                  Type the characters a through g: abdef
                  ab_def_


                  Here's a more complete/advanced hangman example: https://gist.github.com/dbowring/6419866






                  share|improve this answer















                  You could use a list comprehension for this. Here's an example:



                  letters = 'abcdefg'
                  typed = raw_input('Type the characters a through g: ')
                  print ''.join(s if s in typed else '_' for s in letters)


                  Sample Output:



                  Type the characters a through g: abdef
                  ab_def_


                  Here's a more complete/advanced hangman example: https://gist.github.com/dbowring/6419866







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 3 '13 at 5:03

























                  answered Sep 3 '13 at 4:56









                  DanielBDanielB

                  2,0731127




                  2,0731127



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18583929%2fpython-replace-blanks-with-letter%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Darth Vader #20

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Ondo