[TC] Gaming Forums

Full Version: Python code help.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
having a little trouble with my code. This will turn out to be a working 'Hangman' game on Python 3.

Problem: I need it to select one random word from the array and allow the user to guess individual letters. the array has been created and it allows you to guess a letter but then stops running..

no need to download it, any advice is valuable to me, since the stuff on the internet is not really helping me where i need to go.

https://www.sendspace.com/file/e8bh04
Maybe you have to make a "loop" so that when the user types the letter, it loads the code once again?

Like

-At start up: Type the char...
-Type the char
-Result (Yes/No) + Load the first code
-Type the char

Just a thought, reading this with my phone

~F
pretty neat idea, but my code does not recognize any letters nor select a word from the array Sad
Looked at it and just added a three little things.

1) Else argument (if letter is not in the word).
2) Changed letter entry input form to look neater
3) Fixed syntax error (missing colon on if statement)

was-input("Ok, make your first guess")
now-input("Ok, make your first guess: ")

Try having a loop like (Basic python knowledge, beware)

input
if
else
loop input

Not experimented with that, but in theory it should work.

If you want any help just cleaning up the English in this (being a native speaker, I'm willing to help) just ask. Smile

https://www.dropbox.com/s/sp2uazpz3m1itm...v2.py?dl=0
Haha Tongue thanks Matt, will try it out shortly.
Update.

I struggle to know how the script has chosen the word, and the letters i am guessing are actually going towards a full word?

Was not sure if i needed to use a 'for loop' or a 'while loop' when looping the 'guess letter' question?
I'd say for...

Also, for a game like hangman, you may be in slightly over your head...

Try maybe writing down a list of ways to split it i.e. something like
if letter==in whatever you've called your randomly generated word
print "Congratulations"

but, as far as a way to do this simply is concerned...I'm over my head too.
Sidenote-occurred to me today.

While loop...

What you want is something like...

while x != z (Where x is letter input and z is your randomly generated word"

But instead of z you need an embedded if statement to see if said letter (str) is in the word (also str)...
Taking note of what you said Matt, i have created this.

file:///C:/Users/htpc2/Downloads/Hangman_v5.py

Full file DL: https://www.sendspace.com/file/uwg2p0

The code now recognizes correct and incorrect letters.
The code also loops the question over and over 6 times.
The code still needs work to allow the guesses to be stringed together and not on separate lines.

So if anybody out there, can create some sort of string where the letters will be on the same line that would be appreciated greatly
Just a side-note: You can nicely share code (unless its multiple files)
by pasting it online. ie
http://pastebin.com/
or http://paste.ubuntu.com/

It's much more comfortable for the reader than this download hassle.
or just use code and /code in here and spoil when it gets too long:

superintelligent code inside (Click to View)

just some general advice. always make a good plan or diagram before u even start thinking of coding. that helps alot looking at single chunks part by part. for example ns diagram (Wiki Nassi Shneiderman) or at least just a simple scribble:
[Image: 9-4.png]

Quote:The code still needs work to allow the guesses to be stringed together and not on separate lines.

So if anybody out there, can create some sort of string where the letters will be on the same line that would be appreciated greatly

not sure if i understand u correctly, but i would go for a dummy. so if your word is "red" u check length(3) crate dummy "___". then something like
Code:
for i=0 to length(word)
if guess == word[i]
dummy[i] = guess
so if user guesses "e" in the dummy the second position _ gets replaced by a "e" so dummy then is "_e_"

also when guessing a letter i would assure that the letter is only a single letter(eg. length=1) and that the letter is not in right_letters or wrong_letters.
Reference URL's