I'm not sure how to get multiple outputs from a for loop to print on the same line in a window. I'm using the built in Window function from uagame with python3.x. Here's what the code looks like:
for char in a_word:
if char in user_guess:
window.draw_string(char+" ",x, y)
else:
window.draw_string('_ ',x, y)
y = y + font_height
This keeps displaying as:
_
_
_
_
And I want it to print as
_ _ _ _
Any idea how to get each character or _ to display on one line? This is for a WordPuzzle/Hangman type game.
Use this as a example, and hopefully you will implement the same to your code.
for i in range(1,10):
print(i,end=",")
print()
The output looks like
1,2,3,4,5,6,7,8,9,
Define a empty list and append your characters then print all at once
a=[]
for char in a_word:
if char in user_guess:
a.append(char)
else:
a.append(char)
print(a,end=",")
y = y + font_height
Related
I am trying to make a program to print a list separated with space but I do not want the space in the last ?
I tried using string method but want more efficient way to do it.
There is not a lot to go on with the information you have provided but from what I understand may be this could help:-
l = ["Hello", "this", "is", "stackoverflow"]
n = len(l)
for i, ele in enumerate(l) :
x = " "
if i == n-1 :
x = ""
print(ele,end = x)
I am doing an hangman game in python and I'm stuck in the part where I have a random generated word and I'm trying to hide the word by replacing all characters with dashes like this:
generated word -> 'abcd'
hide word -> _ _ _ _
I have done the following:
string = 'luis'
print (string.replace ((string[i]) for i in range (0, len (string)), '_'))
And it gives me the following error:
^
SyntaxError: Generator expression must be parenthesized
Please give me some types
You could try a very simple approach, like this:
word = "luis"
print("_" * len(word))
Output would be:
>>> word = "luis"
>>> print("_" * len(word))
____
>>> word = "hi"
>>> print("_" * len(word))
__
The simplest is:
string = "luis"
"_" * len(string)
# '____'
If you want spaces inbetween:
" ".join("_" * len(string))
# '_ _ _ _'
However, since you will need to show guessed chars later on, you are better off starting with a generator in the first place:
" ".join("_" for char in string)
# '_ _ _ _'
So that you can easily insert guessed characters:
guessed = set("eis")
" ".join(char if char in guessed else "_" for char in string)
# '_ _ i s'
x = input("enter input: ")
if x == "hello":
print ("it does")
How would I detect if x has hello stored even if it has other charaters/strings?
This is as simple as using the in keyword.
x = "123hellomore"
if "hello" in x:
print("Hi there")
This only detects hello, if it is unobstructed so still in one word (not like "**hel*lo")
If entered input is single string then x below will be array of one element, if entered input is space separated strings (string of strings) then x will be array of multiple strings, below code handles both options
x = input("Enter input: ").split()
for y in x:
if y=="hello"
print("it does")
the python code is printing this
___
____
____
____
_
instead of this
____
____
____
____
this is the code some of it doesn't do anything because it has been commented out so just ignore the associated variables
X = 0 #majic number
L = 0 #lines
i = 0 #list intilising varble thing
x = 175 #cordinates are x and y
y = 0 #dont set x&y above 175
Del = x + 1
X = y*176
x = x+X
stuff = []
for L in range(0,7):
for i in range(0,7):
i + 1
stuff.insert(i,"_")
stuff.insert(i,"\n")
i + 1
'''stuff.pop(Del)
stuff.insert(x,"q")'''
print(*stuff, sep='')
in the current settings it supposed to print a 8*8 board. i have fiddled around with the position of the i + 1 but it didn't seem to make a difference.
if someone knows how to get in a alternating pattern that would be great.
when i did that i just got abbbbaaa. i should of mentioned this early but the end goal is to create a game of chess in the terminal.
Just use the increminting operator '+='. i + 1 means nothing in your code and wont have any effect if you removed it. It should be as follows:
for L in range(0,7):
for i in range(0,7):
i += 1
stuff.insert(i,"_")
stuff.insert(i,"\n")
for works very differently in Python than it does in, say, C.
Also, in Python, you only very rarely need to use indices, especially since using iterators is more efficient.
I think you were trying to achieve this with your example:
#!/usr/bin/env python3
x = 3
y = 2
stuff = []
for L in range(y):
for i in range(x):
stuff.append('_')
stuff.append('\n')
# the list now looks like this:
# stuff = ['_', '_', '_', '\n', '_', '_', '_', '\n']
# It contains y*x strings, each length 1.
# This is something like "char **stuff" in C.
output = ''.join(stuff) # this creates '___\n___\n'
print(output)
Note that the variables i and L are actually never used, we are just using the number of items represented by each range to iterate over.
Or if you don't care about the strings being stored in a list and just want to print the rectangle:
#!/usr/bin/env python3
x = 3
y = 2
for L in range(y):
print('_' * x) # by default, print appends '\n' to the end.
'_' * x just repeats the string '_' x times, creating another string, '___'
I'm writing a program to play the game hangman, and I don't think I'm using my global variable correctly.
Once the first iteration of the program concludes after a correct guess, any successive iteration with a correct guess prints the word and all of its past values.
How can I only print the most current value of word? This chunk of code is within a while loop where each iteration gets user input. Thanks!
Code:
word=''
#lettersGuessed is a list of string values of letters guessed
def getGuessedWord(secretWord, lettersGuessed):
global word
for letter in secretWord:
if letter not in lettersGuessed:
word=word+' _'
elif letter in lettersGuessed:
word=word+' '+letter
return print(word)
The Output:
#first iteration if 'a' was guessed:
a _ _ _ _
#second iteration if 'l' was guessed:
a _ _ _ _ a _ _ l _
#third iteration if 'e' was guessed:
a _ _ _ _ a _ _ l _ a _ _ l e
#Assuming the above, for the third iteration I want:
a _ _ l e
Note: This is only a short section of my code, but I don't feel like the other chunks are relevant.
The main problem you are facing is that you are appending your global variable every time you call your function. However, I think you don't need to use a global variable, in general this is a very bad practice, you can simply use the following code considering what you are explaining in your question:
def getGuessedWord(secretWord, lettersGuessed):
return ' '.join(letter if letter in lettersGuessed else '_'
for letter in secretWord)
I also think that it is better if you use a python comprehension to make your code faster.
every time you are calling the function getGuessedWord you are adding to `word, You can not use a global:
secretWord = "myword"
def getGuessedWord(secretWord, lettersGuessed):
word = ""
for letter in secretWord:
if letter not in lettersGuessed:
word=word+' _'
elif letter in lettersGuessed:
word=word+' '+letter
return print(word)
getGuessedWord(secretWord,"")
getGuessedWord(secretWord,"m")
getGuessedWord(secretWord,"mwd")
Or you can solve this by setting word at a constant length, (not as nice and harder to follow) e.g: word='_ '*len(secretWord), then instead of adding to it, replace the letter word=word[:2*i]+letter +word[2*i+1:]
Example here:
secretWord = "myword"
word='_ '*len(secretWord)
def getGuessedWord(secretWord, lettersGuessed):
global word
for i, letter in enumerate(secretWord):
if letter in lettersGuessed:
word=word[:2*i]+letter +word[2*i+1:]
return print(word)
getGuessedWord(secretWord,"")
getGuessedWord(secretWord,"m")
getGuessedWord(secretWord,"w")
getGuessedWord(secretWord,"d")