-2

I have coded a game of Hangman. For unknown letters, it prints a _, but because it is in a while loop it prints a different line. Is there a way to put all the dashes on the same line, so it easier to read?

My code is below:

import time
import random

# Welcoming the user
name = input("What is your name? ")

print("Hello, " + name, "Time to play hangman!")

print(" ")

# Wait for 1 second
time.sleep(1)

print("Start guessing letters...")
time.sleep(0.5)

# Here we set the secret
words = ("pyhton","coding","recycle","dawn","vision","talkative","love","difficulty","late","bake","compact","old","employee","be","bubble","guess","home","urgency","buttocks","shop","fluctuation",
"snack","fling","structure","incentive","enemy","fastidious","overcharge","disagreement","consciousness")
word = random.choice(words)

# Creates an variable with an empty value
guesses = ''

# Determine the number of turns
turns = 10

# Create a while loop

# Check if the turns are more than zero
while turns > 0:

    # Make a counter that starts with zero
    failed = 0

    # For every character in secret word
    for char in word:

    # See if the character is in the players guess
        if char in guesses:

            # Print then out the character
            print(char)

        else:

            # If not found, print a dash
            print("_")

            # And increase the failed counter with one
            failed += 1

    # If failed is equal to zero

    # Print "You Won"
    if failed == 0:
        print("You won, The word was", word)

        # Exit the script
        break

    print

    # Ask the user go guess a character
    guess = input("guess a character: ")

    # Set the players guess to guesses
    guesses += guess

    # If the guess is not found in the secret word
    if guess not in word:

        # Turns counter decreases with 1 (now 9)
        turns -= 1

        # Print "Wrong"
        print("Wrong")

        # How many turns are left
        print("You have", + turns, 'more guesses' )

        # If the turns are equal to zero
        if turns == 0:

        # Print "You Lose"
            print("You Lose")
            print("The word was",word)"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Josh
  • 15
  • 4
  • Josh, if any of the answers below had helped you solve your problem, you should consider accepting it (clicking the checkmark beside it) why? this will help users with the same problem find an answer more quickly and easily. – Thunder Coder Jun 18 '21 at 09:05

3 Answers3

2

The print function has an end parameter which means you can say something like this:

print("text", end=" ")

And instead of moving to a new line, your code will print everything on the same line with spaces between every print.

Note you will have to add this to both print statements:

# see if the character is in the players guess
    if char in guesses:

    # print then out the character
        print(char, end=" ")

    else:

    # if not found, print a dash
        print("_", end=" ")

And you will have to add a "\n" to the front of the text that comes right after the word:

guess = input("\nguess a character: ")
Thunder Coder
  • 104
  • 14
  • That works but now when I guess it wrong the rest of the dashes go on a different line – Josh May 29 '21 at 19:02
  • @Josh Are you adding the `end` parameter both when you are printing dashes and when you are printing characters the user guessed right? – Thunder Coder May 29 '21 at 19:05
1

This should do the trick: Replace your print call for printing "_" with this:

print("_", end = "")  

Example:

for i in range(10):
    print("_", end="")

# Out: __________
Shubham
  • 1,310
  • 4
  • 13
  • That works but now when I guess it wrong the rest of the dashes go on a different line – Josh May 29 '21 at 19:04
  • @Josh It would help if you could send what output you are currently getting for the case you are talking about, and what output your want instead. – Shubham May 29 '21 at 19:15
0

If you want to change the default character at the end of print calls, use the end keyword arg. Example:

>>> word = 'python'
>>> for x in word:
...     print(x, end = ' ')
...
p y t h o n
will-hedges
  • 1,254
  • 1
  • 9
  • 18