Let's discuss computer programming

I've noticed while learning programming that knowing maths gives you a definite edge on your peers. Not only in when you need to do calculations or that kind of stuff, but foremost in understanding things.
 
I dunno if you guys are offering help, but I've a question.

I just started trying to develop a basic command prompt hangman game in Python(I'll try with a GUI once I get the basics working) but I've already run into a problem :p. Here's my code so far:

Code:
import random
import os

possible_Words = ['cat', 'dog', 'mouse']
tries = 10
word = random.sample(possible_Words, 1)

def word_Analysis():
	wordList = list(word)
	print wordList

word_Analysis()

Basically I want, for example if 'cat' was selected, this to be stored in wordList: ['c', 'a', 't'] but instead I just get ['cat']. I've tried the list() function already and it usually returns individual letters, but when I run this program it doesn't work. Does anyone know what I'm doing wrong? Thanks!
 
What happens when you print word?

Does it print ['cat'] or 'cat'? One is a list, the other is a string. Even if it does print 'cat', won't the result of calling list on 'cat' produce ['cat']? Don't you want to make a tuple instead of a list anyway?

My knowledge of Python is sketchy at best ;)
 
What's the difference between a tuple and a list?

And I think I've got it figured out. I need to convert 'word' to a string first, then the list() function divides it into each character.
 
A tuple is fixed size and immutable (i.e. cannot be changed once created) in Python, I thought. Strings are immutable as well, IIRC. Dunno if they are represented as a tuple of characters though.

A tuple is (a, b, c) and a list is [a, b, c], I think.
 
I'm wary of books with "for dummies" in the title ;)

Although I can only blag and wing it when I talk about Java, warpus is your man, I think. I'm a C/C++ guy.
 
Not sure if this is relevant to the thread topic or not, but why are you "wary" of dummies books? I find them to be helpful.

edit: not arguing with you though. I mean, you are the expert here. If you know of a better book than the one I mentioned, please tell me about it.
 
I've always wondered people who identify themselves as the target group of those. "I'm dummy, so I should buy this!". :)

My humble opinion on school books is that it's better to go to library, browse some books and borrow those which look good. Unless money burns your pockets. If the course hasn't started yet, you're more likely to find something. I buy usually only absolutely necessary ones or cheaps.
 
Yeah, go to a library, top advice as always from Atticus.

As I said, I don't know much Java, so any book recommendation from me would be guesswork at best. If Herb Shildt writes books about it though, avoid like the plague ;)

http://www.catb.org/jargon/html/B/bullschildt.html
 
A tuple is fixed size and immutable (i.e. cannot be changed once created) in Python, I thought. Strings are immutable as well, IIRC. Dunno if they are represented as a tuple of characters though.

A tuple is (a, b, c) and a list is [a, b, c], I think.
Yeah you're right, in Python there's [lists], (tuples), and {sets}. I can't think of why I would want to use a tuple or set instead of a list though. What are they there for?

Anyways completed my hangman game :) It was surprisingly easy to work though, because I could test everything I wanted to do interactively with Python. Here's the code if you guys have any suggestions on making it less messy:

Spoiler :
Code:
import random


possibleWords = ['augsburg', 'braunschweig', 'darmstadt', 'erfurt', 'schwerin', 'dortmund', 'karlsruhe', 'bielefeld', 'duisburg', 'frankfurt', 'heidelberg', 'mannheim', 'mainz', 'paderborn', 'passau', 'bremen', 'bonn', 'dresden', 'stuttgart', 'hannover', 'bochum', 'hamburg', 'berlin']
tries = 6
selectedWord = random.choice(possibleWords)
wordLen = len(selectedWord)
wordStatus = wordLen * ['_ ']	
numberCorrect = 0
alreadyGuessed = []

def current_status():
	print "\nCURRENT STATUS"
	print "--------------"
	print "Tries remaining: %d\n" % tries
	print "WORD:\n"
	print ''.join(wordStatus)	
	
while tries > 0 and numberCorrect != wordLen:
	current_status()
	print "Type in a letter and press ENTER."
	guess = raw_input('->')
	if len(guess) == 1 and guess.isalpha() == True:
		if selectedWord.find(guess) != -1 and guess not in alreadyGuessed:
			print "Correct! The letter %s is in the word." % guess
			alreadyGuessed.append(guess)
			for i in range(wordLen):
				if guess == selectedWord[i]:
					wordStatus[i] = "%s " % guess
					numberCorrect = numberCorrect + 1
		elif guess in alreadyGuessed:
			print "You already guessed that letter. Please choose another."
		elif selectedWord.find(guess) == -1:
			print "Sorry, the letter %s is not in the word." % guess
			tries = tries - 1
	
	else:
		print "Invalid guess. Enter a single letter please."
if tries == 0:
	current_status()
	print "Sorry you lose! Your word was %s" %selectedWord
if numberCorrect == wordLen:
	current_status()
	print "Congratulations you've won! Your word was %s" % selectedWord
 
Noo, but hangman never is :p Next "version" I'll implement a way to make the first letter capitalized when displayed, and also a replay option.
 
Nice one.

Tuples are more efficient than lists, but that's only important if your program is slow. And you only know if your program is slow when you run a profiler, follow the 3 rules of optimisation, from Djikstra:

1) Don't optimise
2) Don't optimise - yet
3) Only optimise something a profiler has told you is slow

;)
 
C#, VB.NET and Java add automatic garbage collection which makes it easier to make less mistakes. With C, you need to really know what you are doing, most programming languages allow you to shoot yourself in the foot, with C it's easy to blow your whole leg off. C++ allows you to blow your leg off and re-use the bullet.

:lol: Nice analogy. It sounds somewhat familiar, but congrats if you did come up with it yourself.

Java. VB sucks (whether .NET or not).

I didn't understand why people hated VB until I started using it on a very large, commercial application. Now, I understand why. VB really does have a personality of its own once you start using in on larger scales. I wish Microsoft had released a VB 7. Even if it didn't add any new features, if it had fixed the most annoying bugs and glitches it would have been well worth it.

Can't comment on VB .NET as a language, though. I don't anticipate I'll be learning it anytime soon.

Ok. Well I'm a computer science major however I am finishing my associates degree right now (only basic classes, nothing with computers).

Next semester I will start on my major and I have a choice between a seminar in JAVA or VB.NET. Which would you say is better?

I'd say it depends on what you want to go into. If you like the idea of Windows programming, especially if you don't have experience there so far, VB.NET might be a good choice. Java is cross-platform and probably a better choice at the enterprise.

How important it is to learn swing in java?

Spoiler pre-emptive :

I find techno to be fairly effective for Java programming, so I'd say not particularly. Though if you dislike techno, YMMV.

(serious answer follows) Depends a lot on what you want to do, and whether it would benefit from those graphics. It's not a bad graphics platform, though avoid NetBeans' old GUI builder like the plauge as it drives people mad (their newer one seems less maddening) and hand coding is friendlier. But it may be completely unnecessary depending on what you want to do, and there are competing graphics frameworks (though I'm not familiar with them).

I've always wondered people who identify themselves as the target group of those. "I'm dummy, so I should buy this!". :)

Me too. I've never bought one of those books in part because of the implication that if you do, you are a dummy.
 
Can't comment on VB .NET as a language, though. I don't anticipate I'll be learning it anytime soon.



I'd say it depends on what you want to go into. If you like the idea of Windows programming, especially if you don't have experience there so far, VB.NET might be a good choice. Java is cross-platform and probably a better choice at the enterprise.

It's like ParadigmShifter said, VB.NET is C#, but with worse syntax.
 
Nice one.

Tuples are more efficient than lists, but that's only important if your program is slow. And you only know if your program is slow when you run a profiler, follow the 3 rules of optimisation, from Djikstra:

1) Don't optimise
2) Don't optimise - yet
3) Only optimise something a profiler has told you is slow

;)

That used to/still kinda is my greatest flaw.
 
How important it is to learn swing in java?

Spoiler pre-emptive :

a bit late, but personally, if you haven't learned anything about Swing yet, I wouldn't bother and just learn JavaFX instead as it's bound to replace Swing not too far away...unless you have a very concrete need to program a GUI in Java soonish.
 
Thanks for the advice, guys! :goodjob:

I did learn some swing, as it was part of the programming course I'm participating, and it was easier than the overwhelming size of the documentation suggested.

The result didn't look very good, but I suppose the purpose was to learn the basics of GUIs rather than make fine ones. A tutor told me that SWT would be better (or it couldhave been AWT, GWT or something like that).
 
Top Bottom