Skip to main content
GameDev.net gamedev.net
🔒 Locked

I need help learning to program (python)

Started by sleepingtom Jun 24, 2011 at 7:08 PM 14 replies 3.2k views
Original Post
sleepingtom
sleepingtom
Hello im trying to learn to program so i picked to start python because it is easier than c++ or java
and i started with tutorials and i only learned the basics from them and the best thing i can do now is hangman and that was with help from my teacher at shcool
can some one please help me with getting better

p.s. here is my program
sleepingtom
sleepingtom
import random
HANGMANPICS = ['''

+---+
| |
|
|
|
|
=========''', '''

+---+
| |
O |
|
|
|
=========''', '''

+---+
| |
O |
| |
|
|
=========''', '''

+---+
| |
O |
/| |
|
|
=========''', '''

+---+
| |
O |
/|\ |
|
|
=========''', '''

+---+
| |
O |
/|\ |
/ |
|
=========''', '''

+---+
| |
O |
/|\ |
/ \ |
|
=========''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split()

def getRandomWord(wordList):
# This function returns a random string from the passed list of strings.
wordIndex = random.randint(0, len(wordList) - 1)
return wordList[wordIndex]

def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
print(HANGMANPICS[len(missedLetters)])
print()

print('Missed letters:', end=' ')
for letter in missedLetters:
print(letter, end=' ')
print()

blanks = '_' * len(secretWord)

for i in range(len(secretWord)): # replace blanks with correctly guessed letters
if secretWord in correctLetters:
blanks = blanks[:i] + secretWord + blanks[i+1:]

for letter in blanks: # show the secret word with spaces in between each letter
print(letter, end=' ')
print()

def getGuess(alreadyGuessed):
# Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.
while True:
print('Guess a letter.')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print('Please enter a single letter.')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a LETTER.')
else:
return guess

def playAgain():
# This function returns True if the player wants to play again, otherwise it returns False.
print('Do you want to play again? (yes or no)')
return input().lower().startswith('y')


print('H A N G M A N')
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False

while True:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)

# Let the player type in a letter.
guess = getGuess(missedLetters + correctLetters)

if guess in secretWord:
correctLetters = correctLetters + guess

# Check if the player has won
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print('Yes! The secret word is "' + secretWord + '"! You have won!')
gameIsDone = True
else:
missedLetters = missedLetters + guess

# Check if player has guessed too many times and lost
if len(missedLetters) == len(HANGMANPICS) - 1:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
gameIsDone = True

# Ask the player if they want to play again (but only if the game is done).
if gameIsDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameIsDone = False
secretWord = getRandomWord(words)
else:
break




JosephParrilla
JosephParrilla
You should just get a book. Go to amazon and sort by bestselling, there are a bunch to choose from. Its really hard for someone to tell you what to do next, you seem to be on the right track, its just a matter of more practice with what you know, and more learning what you dont know. Good luck !
Never, ever stop learning.
- Me
sleepingtom
sleepingtom
i need something free to learn im only in high school and dont have a job and my parents dont allow me to order books from online and i would like to find something free if not sold in book a millon or barnes a nobles can you recomend anything in those criteria?
JosephParrilla
JosephParrilla
Well you can definitely find online tutorials. As far as a book... I can't say which books your barnes and noble would have, just go check it out.

There are definitely plenty of good online tutorials though, just search around. Sorry I don't have more time to help. Python is popular, it shouldn't be too hard. Good luck
Never, ever stop learning.
- Me
beatlefan
beatlefan
Check out thenewboston on youtube. He has some python tutorials if you check in his playlists. I never watched his python tutorials, but he does a really good job explaining things in other tutorials I've watched
BrianFerentheil
BrianFerentheil

You could try How to Think Like a Computer Scientist: Learning with Python, available free online.


I just started learning about coding, and that open book source is fantastic. I am having an issue though. I am using the free version of python (idk if that matters), but in the introduction when it is stating that:
-
print "hello, world!"
-
will generate the quoted texted, I only get syntax errors. I feel like I am doing something wrong but don't have anyone to teach me the simple things to get started.
If anyone can point out what I'm doing wrong in a clear, concise way, I would greatly appreciate it.
fcoelho
fcoelho
in the introduction when it is stating that:
-
print "hello, world!"
-
will generate the quoted texted, I only get syntax errors.


If a simple

print "Hello, world"

fails with a syntax error, that's most likely because you're running Python version 3 instead of Python 2. In that case, check the link for the third edition of the book, which uses Python 3.
BrianFerentheil
BrianFerentheil

[quote name='Kpyrate' timestamp='1309116110' post='4827974']in the introduction when it is stating that:
-
print "hello, world!"
-
will generate the quoted texted, I only get syntax errors.


If a simple

print "Hello, world"

fails with a syntax error, that's most likely because you're running Python version 3 instead of Python 2. In that case, check the link for the third edition of the book, which uses Python 3.
[/quote]

Aha, yes that seems to look better. Thank you very much!
Durakken
Durakken
could go to game maker, look at their tutorials and convert them to python. I did that with one of their programs and it was interesting to do since you are being given the logic more or less, but having to figure out how to accomplish the automatic parts so in a way it helps you learn from both ends, the logic side and the programming side.
BrianFerentheil
BrianFerentheil

could go to game maker, look at their tutorials and convert them to python. I did that with one of their programs and it was interesting to do since you are being given the logic more or less, but having to figure out how to accomplish the automatic parts so in a way it helps you learn from both ends, the logic side and the programming side.


I am still trying to learn the ropes of Python. I am using the free version that someone recommended to me if I was interested in learning, that it was a good way to start practicing. I have never done any code writing, done so I am clueless to most of what I am learning, but I pick up on rules and principles quickly once I see how they work. That being said, right now I am stuck trying to make and/or run scripts. I downloaded Programmer's Notepad and tried following the openbooksource directions on that, but I continuously run into syntax errors when I try to run it. I went looking in Python31/Tools/Scripts.txt and read through the list of scripts that (I assumed) would be functional, but I could not get them to run either.
The format of input I am trying to run it with is:
-----------
$ python filenamehere.py
-----------
it is how they example it but I cannot follow along the instructions when I can't get these functions to work like how they are shown. Does anyone know what I am doing wrong?
sleepingtom
sleepingtom
a have a question the python interperter and idle are open source which one is for sale?

umm the question asked i dont think that the $ sign is syntactaly correct in 3 i think that was for two

but i use 3.2 and 2.5
Durakken
Durakken
I agree, you should just be using the IDLE and pygame. Those are the standards that most people use for Python.

I'd say the best way to learn coding is to do small focus driven programs.
Find tutorials for the various basic programming stuff. (though i don't recommend it you can use other languages and convert to python)
And then once you have it working like it says it is supposed to, play with it a bit and see what happens.

Off the top of my head, you should learn and understand the following

if...elif
while loop
for loop
variables (global and local)
functions
classes/objects
nesting
modules
arrays
strings
Files

Don't really much else than that for most programming.


I've never seen the merit of using things like WYSIWYG, Programmer's Notepad, and those type programs... I'd prefer to program in notepad if it lets me... and Python's IDLE pretty much gives you notepad that color codes code and has some useful menu commands which are useful without cluttering.
BrianFerentheil
BrianFerentheil

a have a question the python interperter and idle are open source which one is for sale?

umm the question asked i dont think that the $ sign is syntactaly correct in 3 i think that was for two

but i use 3.2 and 2.5


If it's the $ sign that is incorrect with 3.1, what is the proper input?

------

(edit) After some tinkering in IDLE, I was working in a [New Window] and I found the [Run Module/F5] will bring Shell to the top and give me the display I have been looking to get. Is this the only way to open it though? The other instructions I have seen said there was a command to bring up the program without loading other windows..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.