Convert for loop/print output to string? Python -
def reveal(): guessright = 0 letter in secretword: if letter == usertry: guessright = guessright + 1 print usertry, else: print "_", return guessright reveal()
when using reveal() _ _ _ _ _ printed loop- there way of converting prints string, or getting function return 2 outputs, integer guessright, , stuff printed while reveal() running?
sure, there lots of ways -- simplest use list hold data (appending new data rather printing) , ''.join
@ end:
def reveal(): text = [] guessright = 0 letter in secretword: if letter == usertry: guessright = guessright + 1 text.append(usertry) else: text.append('_') return guessright, ''.join(text) reveal()
here return tuple
python's way of returning multiple values. can unpack tuple in assignment if wish:
guessright, text = reveal()
Comments
Post a Comment