1st programming problem
alright well im using python to write this but i think it can be direceted to any types of programming.
i'm trying to make hangman using python. i haven't started yet but im just trying to plan out how im going to do it. this is my train of thought. i know im going to be way off but try to stay with me here. right now i only know one way to display something so until i learn something new im going to have to use "print."
im going to have the word the person is trying to find stored in list lets say... listA (i even forget how to write lists... thats how sad i am). now this is only for the part to display what the word is. like to display the letters that you have guessed. i'm going to put it in a for loop so it would look like this.
for x in listA:
if x == 1: print character
else: print "_"
this is what im going for. the for loop should run through every letter in listA. it will check to see if a 1 value has been stored to x. if the x = 1 it will print the given character to that spot. otherwise it will print _. for example... if the word is booth and the person guessed "o", it would return. _ o o _ _ _.
here are the problems that i already see.
#1. i don't know if the design for the for loop will work.
#2. i need to change the x == 1 into something else. if the first letter is a and you guess a it will print all of the characters because x = 1
#3. i need to change the print character to something else. i just put that because i have no idea about what to put there.
alright after i wrote all of that i thought of a possible solution. i could have x = the letter guessed. then i would turn the letter guessed into a numerical value (like a is 1) and run it through like that. then it would check to see if the numerical value of the letter guessed was the same as the numerical values of the letters in the list. if they are the same they will print and if not they will be left out.
if i lost you tell me cause i probably did. i need any help or critisism i can get. i need to figure this out though because im doing it for school.
AI Summary
16 Comments
ok well now im onto building the design for everything. i've kinda run into a problem. well its not really a problem but a question of proper programming techniques. i've started to program with tkinter(tcl or tk). at the moment im making a keyboard for my game. this is what i've written so far.
from Tkinter import *
root = Tk()
root.geometry('600x200')
c1 = "blue"
Button(root, text = "Q", fg = c1, borderwidth = 3, relief = 'raised', width = 5, command = exeQ).grid(row = 3, column = 0, columnspan = 19)
Button(root, text = "W", fg = c1, borderwidth = 3, relief = 'raised', width = 5, command = exeW).grid(row = 3, column = 20, columnspan = 19)
Button(root, text = "E", fg = c1, borderwidth = 3, relief = 'raised', width = 5, command = exeE).grid(row = 3, column = 40, columnspan = 19)
Button(root, text = "R", fg = c1, borderwidth = 3, relief = 'raised', width = 5, command = exeR).grid(row = 3, column = 60, columnspan = 19)
Button(root, text = "T", fg = c1, borderwidth = 3, relief = 'raised', width = 5, command = exeT).grid(row = 3, column = 80, columnspan = 19)
Button(root, text = "Y", fg = c1, borderwidth = 3, relief = 'raised', width = 5, command = exeY).grid(row = 3, column = 100, columnspan = 19)
root.mainloop()
now theres my problem. it is going to be very reptitious. is there anyway that i can make the buttons using a for loop? im not very familair with for or spelling familair so don't ask me to do either one of those. i was thinking i could use a for statement to save all of the letters to a list. then i could use a for statement to make all of the buttons. when you click the button it will run a command that saves the letter from the list to my program i have for hangman.
word = ["h","e","l","l","o"]
len1 = len(word)
blanks = ["_","_","_","_","_"]
print ''.join(word)
print ' '.join(blanks)
def lb():
guess = raw_input("Choose a letter. ")
count1 = 0
correct = 0
while count1 <= len1:
if guess == word[count1]:
blanks[count1] = guess
correct = 1
count1 = count1 + 1
lb()
if correct == 1: lb()
else: print "You lose"
print ''.join(word)
print ' '.join(blanks)
alright i wrote that to see if somethings would work the way i wanted them to. they didn't. when i run it i get an error. this is what it looks like.
hello
_ _ _ _ _
Choose a letter. h
Traceback (most recent call last):
File "C:\python2.4\hangman1.py", line 15, in -toplevel-
lb()
File "C:\python2.4\hangman1.py", line 11, in lb
if guess == word[count1]:
IndexError: list index out of range
it says the list index is out of range on line 11. that part should work. i've tested it outside of that program and it works. guess should equal h. count1 should equal 0. it should check to see if the first index in word is h. don't know whats wrong. i might just rewrite it using a for statement.
i copy and pasted from another place so i don't know how or why it looks the way it does...
while count1 <= len1 is a classical 'off by one' logical error
Subscript out of range means you are trying to access the array variable "word" out of it's original range you declared it for.
length of word = 5
word[0] = h
word[1] = e
......
word[4] = o
word[5] = subscript error
thanks for the help. i saw that 2 days after i posted.
thought about it somemore and this is what i got. by the way i do know how to use arrays. however i don't know how to manipulate the strings so i came up with this on my own.
word = house
thats the word you are suppose to find. then i find the length of word (this case it would be 5). then i would put it into a while loop.
a = 0
while a <= 4:
list.append(word(a))
a = a + 1
now to my understanding this loop would continue 5 times. the first time it would assign h to the list. the 2nd time it would assign o. and so on and so forth. is that anywhere near being correct?
ok, so a string is treated just like a list in python. just found out about that.
some corrections:
word = 'house'
or
word = "house"
you access a character of a string as word[a] where a is an integer.
alright well a few things then. i have no idea what a flag is. never heard of it before nor do i know how to use it.
i've been thinking about how i am going to do it by using lists. the only problem is that i have no idea about how to seperate the list to make every letter seperate. for example... if list1 = ['treehouse'] i need to turn it into ['t','r','e','e','h','o','u','s','e',]. is there i just can't have it saved into those two different lists because i'm going to have it random. so like next time the word might not be treehouse.
is this one of those things you are going to make me figure out for myself?
flag is a boolean status. true false
"is this one of those things you are going to make me figure out for myself?"
well i guess if you're asking that question at this point , i could have posted the answer and you wouldn't even have known it :o)
actually i dont know python, sound's like you're on the right track
you'll need to figure out how to use Python's string manipulation library to convert a string into an array.
str1 = "treehouse"
is a string,
list1 = ["t","r","e","e","h","o","u","s","e"] is a "list" (called an "array" in most languages, not sure about python)
in Java, list[0] is "t", list[5] is "o" (Note that array indexes start at 0 in most languages)
Python might allow you to treat the string as an array of characters without conversion.
i.e. str1[1] is "r" (I think JavaScript works this way)
you really need to learn the syntax of the language (how to do loops, conditional blocks/operators, work with arrays), then learn about the standard libraries (i.e. printing out to the screen, manipulating strings, more advanced math functions)
that's really what's most important.
when I say "most languages" I mean "most languages I've used". heh.
well then if you aren't going to help can you at least tell me if my thought process is right? or if im on the right track? or if im getting better? you know positive things.
i thought about it some more. i think im going to use 2 different lists. the first list will be the word. it will be run to check the word to see if the guess fits any of the letters of the original word. the second list will be filled with '_'. if the correct letter was chosen i would replace the '_' with the letter.
i think that one's a little better. have to go to school.
Dean's right, you're not going to learn much by us telling you....
You learned what tools you have right? Use your imagination and figure out how to use them.
Plus, there's a million in one ways to code for this problem. Take a good guess. Go after it. If it doesn't work you will learn 250% more than you did since you started. IMO you will best learn with trial and error. My favorite exercises in books are the ones with mistakes, makes you think and learn from the mistake.
Hint: use flags (true/false) but that's one of a gazillion methods
although I'd love to help, I wonder if you might learn more figuring it out yourself...
heh i think i speak for all of us decently seasoned coders
"answers" are never truly appreciated >:\
this one guy took C++ here at an online university, i don't think he learned from one thing i taught him, what a shame