• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:10
CEST 01:10
KST 08:10
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18
Community News
Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris34Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195
StarCraft 2
General
BoxeR's Wings Episode 2 - Fan Translation Greatest Players of All Time: 2025 Update #1: Maru - Greatest Players of All Time A Eulogy for the Six Pool Geoff 'iNcontroL' Robinson has passed away
Tourneys
$5,000 WardiTV Summer Championship 2025 Maestros of The Game—$20k event w/ live finals in Paris $5,100+ SEL Season 2 Championship (SC: Evo) Esports World Cup 2025 Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
External Content
Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Post ASL20 Ro24 discussion. No Rain in ASL20? How do I speak directly to Coinbase?1-(888)-419-97 Recent recommended BW games
Tourneys
[ASL20] Ro24 Group D [ASL20] Ro24 Group E Small VOD Thread 2.0 [Megathread] Daily Proleagues
Strategy
Muta micro map competition Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread General RTS Discussion Thread Nintendo Switch Thread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The year 2050 European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s) Gtx660 graphics card replacement
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
How Culture and Conflict Imp…
TrAiDoS
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
INDEPENDIENTE LA CTM
XenOsky
[Girl blog} My fema…
artosisisthebest
Customize Sidebar...

Website Feedback

Closed Threads



Active: 967 users

The Big Programming Thread - Page 208

Forum Index > General Forum
Post a Reply
Prev 1 206 207 208 209 210 1031 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Kambing
Profile Joined May 2010
United States1176 Posts
December 03 2012 20:18 GMT
#4141
On December 04 2012 05:04 Recognizable wrote:

original = raw_input('Enter a word:')
word = original.lower()
pyg = 'ay'
new_wordV = word+pyg #w/Vowel
new_wordC = original[1:]+word[0]+pyg #w/Consonant
first = original[0]
if len(original) > 0 and original.isalpha():
if first == "a" or first == "i" or first == "e" or first == "o" or first == "u" or first == "A" or first == "I" or first == "E" or first == "O" or first == "U" :
print new_wordV
else:
print new_wordC
else:
print 'empty'


Can this be made to look easier/less lines of code? If so could you give me some pointers what I could do(don't do it for me) Because I once posted something here 5+ lines and it was all done in like 2 :O. Especially the line with all the first==, it bugs me. I tried messing around a bit but nothing worked.


(1) Don't use locals unless its necessary. In particular, you shouldn't need to pre-compute new_wordV and new_wordC until you actually need them (i.e., inside of the if-statement).

(2) The inner if-guard can be greatly simplified by using lower() as well as set inclusion (e.g., x in [1, 2, 3]) rather than repeated equality checks.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
December 03 2012 20:30 GMT
#4142
On December 04 2012 05:04 Recognizable wrote:

original = raw_input('Enter a word:')
word = original.lower()
pyg = 'ay'
new_wordV = word+pyg #w/Vowel
new_wordC = original[1:]+word[0]+pyg #w/Consonant
first = original[0]
if len(original) > 0 and original.isalpha():
if first == "a" or first == "i" or first == "e" or first == "o" or first == "u" or first == "A" or first == "I" or first == "E" or first == "O" or first == "U" :
print new_wordV
else:
print new_wordC
else:
print 'empty'


Can this be made to look easier/less lines of code? If so could you give me some pointers what I could do(don't do it for me) Because I once posted something here 5+ lines and it was all done in like 2 :O. Especially the line with all the first==, it bugs me. I tried messing around a bit but nothing worked. Also I'd like to add coding yourself(mostly) is really fun, I just spent 10 minutes pressing in random words and I was amazed at how it worked haha.


I don't know enough python to tell you the exact code, but for your "first == ..." line, i prefer to use arrays and then a function like indexof() or in_array() or whatever it might be called in python. In your specific case, the string "aieouAIEOU" with a string indexof() or strpos() function would also work.
Recognizable
Profile Blog Joined December 2011
Netherlands1552 Posts
Last Edited: 2012-12-03 21:21:34
December 03 2012 20:51 GMT
#4143
On December 04 2012 05:18 Kambing wrote:
Show nested quote +
On December 04 2012 05:04 Recognizable wrote:
original = raw_input('Enter a word:') 
word = original.lower()
pyg = 'ay'
new_wordV = word+pyg #w/Vowel
new_wordC = original[1:]+word[0]+pyg #w/Consonant
first = original[0]
if len(original) > 0 and original.isalpha():
if first == "a" or first == "i" or first == "e" or first == "o" or first == "u" or first == "A" or first == "I" or first == "E" or first == "O" or first == "U" :
print new_wordV
else:
print new_wordC
else:
print 'empty'


Can this be made to look easier/less lines of code? If so could you give me some pointers what I could do(don't do it for me) Because I once posted something here 5+ lines and it was all done in like 2 :O. Especially the line with all the first==, it bugs me. I tried messing around a bit but nothing worked.


(1) Don't use locals unless its necessary. In particular, you shouldn't need to pre-compute new_wordV and new_wordC until you actually need them (i.e., inside of the if-statement).

(2) The inner if-guard can be greatly simplified by using lower() as well as set inclusion (e.g., x in [1, 2, 3]) rather than repeated equality checks.


1) and 2) Do you mean this?

vowels=["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
original = raw_input('Enter a word:').lower()
if len(original) > 0 and original.isalpha():
if original[0]in vowels:
print original()+"ay"
else:
print original[1:]+original[0]+"ay"
else:
print 'empty'



I don't know enough python to tell you the exact code, but for your "first == ..." line, i prefer to use arrays and then a function like indexof() or in_array() or whatever it might be called in python. In your specific case, the string "aieouAIEOU" with a string indexof() or strpos() function would also work.


I understand some of these words ^.^
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2012-12-03 21:37:12
December 03 2012 20:55 GMT
#4144
On December 04 2012 05:04 Recognizable wrote:

original = raw_input('Enter a word:')
word = original.lower()
pyg = 'ay'
new_wordV = word+pyg #w/Vowel
new_wordC = original[1:]+word[0]+pyg #w/Consonant
first = original[0]
if len(original) > 0 and original.isalpha():
if first == "a" or first == "i" or first == "e" or first == "o" or first == "u" or first == "A" or first == "I" or first == "E" or first == "O" or first == "U" :
print new_wordV
else:
print new_wordC
else:
print 'empty'


Can this be made to look easier/less lines of code? If so could you give me some pointers what I could do(don't do it for me) Because I once posted something here 5+ lines and it was all done in like 2 :O. Especially the line with all the first==, it bugs me. I tried messing around a bit but nothing worked. Also I'd like to add coding yourself(mostly) is really fun, I just spent 10 minutes pressing in random words and I was amazed at how it worked haha.


+ Show Spoiler +

word = raw_input("Enter a word: ").lower()

if (not word.isalpha()) or word.isspace():
print "Invalid input for word generation"

else:
vowels_lowercase = ["a", "e", "i", "o", "u"]
pyg = "ay"

if word[0] in vowels_lowercase: #word with first letter as vowel
print word+pyg
else: #word with first letter as consonant
print word[1:]+word[0]+pyg

raw_input()


I don't remember how pig latin works so there could be some optimizations beyond this with more knowledge of exactly the rules.

You use "in" to automatically check for the existence of something in a list.

You do the exception handling at the beginning of the code.

You use the isalpha() and isspace() functions on strings to make it more verbose. (netherh said isspace() is in isalpha()? lolol) (oh, isalpha checks for empty string, not spaces. kk xD) (wait no I still think isspace() might be unnecessary, better safe than sorry?)

You use not isalpha() because it makes a lot more sense and is verbose as an exception handler; if it's not alphabetical, then it's an exception, instead of having just an "else" for everything.

You put the variables in the scope in which they're used, so if you don't use them, you didn't create them needlessly.

You can use sequences of string methods like lower() and upper() in one line.

If you've already used lower() on a string, it can't possibly have uppercase letters, right? So no need to check for those.

On December 04 2012 05:51 Recognizable wrote:
Show nested quote +
I don't know enough python to tell you the exact code, but for your "first == ..." line, i prefer to use arrays and then a function like indexof() or in_array() or whatever it might be called in python. In your specific case, the string "aieouAIEOU" with a string indexof() or strpos() function would also work.


I understand some of these words ^.^


They're not really relevant for this situation. They look for characters in strings, which is different from this problem. The same thing for indexof() and strpos() is find() in Python. I'm not exactly sure what indexof() is though. I assume it's the same as strpos().

Python has some beautiful special things you should use. Using the "element in list" code automatically makes your life so much nicer.

Your ultimate goal in Python is writing really succinct and verbose code. Something Python programmers love is that your Python code can be read like a book if you write it really well.


@netherh, <insert huge debate about returning early versus else propagation> :D
also, oops, you caught my bad habit of putting those list catchers as globals.
There is no one like you in the universe.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2012-12-03 20:57:13
December 03 2012 20:56 GMT
#4145
--doublepost--
There is no one like you in the universe.
Perscienter
Profile Joined June 2010
957 Posts
December 03 2012 21:09 GMT
#4146
On December 04 2012 05:30 Morfildur wrote:I don't know enough python to tell you the exact code, but for your "first == ..." line, i prefer to use arrays and then a function like indexof() or in_array() or whatever it might be called in python. In your specific case, the string "aieouAIEOU" with a string indexof() or strpos() function would also work.

That's not Python jargon. You are probably talking about lists.


Well someone posted his solution any way.

Make sure to go through the tutorial, especially control flow tools, data structures etc.

And please get used to PEP 8, especially the maximum line length, which does make sense and the naming conventions.
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
Last Edited: 2012-12-03 21:26:32
December 03 2012 21:17 GMT
#4147
On December 04 2012 05:04 Recognizable wrote:

original = raw_input('Enter a word:')
word = original.lower()
pyg = 'ay'
new_wordV = word+pyg #w/Vowel
new_wordC = original[1:]+word[0]+pyg #w/Consonant
first = original[0]
if len(original) > 0 and original.isalpha():
if first == "a" or first == "i" or first == "e" or first == "o" or first == "u" or first == "A" or first == "I" or first == "E" or first == "O" or first == "U" :
print new_wordV
else:
print new_wordC
else:
print 'empty'


Can this be made to look easier/less lines of code? If so could you give me some pointers what I could do(don't do it for me) Because I once posted something here 5+ lines and it was all done in like 2 :O. Especially the line with all the first==, it bugs me. I tried messing around a bit but nothing worked. Also I'd like to add coding yourself(mostly) is really fun, I just spent 10 minutes pressing in random words and I was amazed at how it worked haha.


What Kambing said. Note that you're not really consistent with your lower / upper case between vowel / consonant versions due to using original[1: ] in one, and word in the other (kinda nitpicking, but thought I'd point it out). You also access word[0] before checking if it's empty.

Personally I'd go with this (python 3.x):

original = input('Enter a word: ').lower()

if not original.isalpha() or original.isspace():
print("invalid")
return

vowels = ["a", "e", "i", "o", "u"]
pyg = "ay"

if original[0] in vowels:
print(original + pyg)
else:
print(original[1:] + original[0] + pyg)

Note that the isalpha() check also catches empty strings.

@Blisse: haha. XD (*steals your isspace check*).
Recognizable
Profile Blog Joined December 2011
Netherlands1552 Posts
Last Edited: 2012-12-03 21:32:05
December 03 2012 21:26 GMT
#4148
So many ways to write something, I'm kinda confused. On another note, can anyone recommend a good book for learning to program?


vowels=["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
original = raw_input('Enter a word:').lower()
if len(original) > 0 and original.isalpha():
if original[0]in vowels:
print original()+"ay"
else:
print original[1:]+original[0]+"ay"
else:
print 'empty'


This is what I ended up with, anyway I have to go sleep.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2012-12-03 23:53:14
December 03 2012 21:48 GMT
#4149
On December 04 2012 06:26 Recognizable wrote:
So many ways to write something, I'm kinda confused. On another note, can anyone recommend a good book for learning to program?


vowels=["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
original = raw_input('Enter a word:').lower()
if len(original) > 0 and original.isalpha():
if original[0]in vowels:
print original()+"ay"
else:
print original[1:]+original[0]+"ay"
else:
print 'empty'


This is what I ended up with, anyway I have to go sleep.



There are always a lot of ways to write something; there is no "best way." It always depends on what you want to achieve with your code.

However, there are a lot of "bad ways."

One such way is writing code with lots of redundancies and it's always frowned upon.

For example, if you call lower() on a string, then you check if the string has any uppercase letters. It even sounds redundant, no? as how can a lower cased string have uppercase letters anyways?

Using len(word) > 0 is redundant if you also have a isalpha() check, since isalpha fails on empty strings. (I think isspace() is unnecessary though and I'm not sure if isalpha() fails for sentences with spaces in them, as our assumption is this program will only be fed words and not strings.)

So those are some good practices.

Other good practices is also making use of special functions inside the programming language you're working on; in this example, "element in list", instead of writing your own ways to check it, like where you used "first_letter == ... ".


In Python, the favourite practice is usually make your code as verbose and succinct as possible. Make it highly readable, but also very short and sweet. If I read through any part of your code, it should be obvious what you're doing, but also that your way of doing that is the fastest and most maintainable way to do it.

When we reversed your if...else position, we did it because now it's perfectly clear when you handle your errors and what you determined are errors. They're in the same block of code.


The best way to learn is actually just to code a lot a lot in the language, write lots of mini-programs, keep up-to-date knowledge of the language, and look at good stacks exchange solutions to your problems and why they're good.

If you want to learn about programming in general, scope, exceptions and such, there are some books, but best practices come from just writing a lot of code, and debugging your horrible code when it doesn't work.


tl;dr It's a lot more about understanding why you would use one piece of code instead of an another.
There is no one like you in the universe.
Craton
Profile Blog Joined December 2009
United States17250 Posts
Last Edited: 2012-12-03 23:29:28
December 03 2012 23:29 GMT
#4150
On December 03 2012 20:10 Tobberoth wrote:
Show nested quote +
On December 03 2012 19:36 teamamerica wrote:
On December 03 2012 08:42 Morfildur wrote:
On December 03 2012 08:24 darkness wrote:
On December 03 2012 06:09 Fyodor wrote:
On December 03 2012 05:56 darkness wrote:
Java: Is it possible to send output across the network to every user within a linked a list? If yes, how? I've tried some google search, but I found nothing.

for each loop? I'm not sure I understand at what level your problem is.


Some users are added to a linked list. I want to deliver a msg to all of them. They're all connected. loop is ok, but how do I make sure everyone receives the message? E.g. out.println("text") doesn't ask for receiver.


That depends on what you want to send it through...


Post more code! Put it on github and link for easiness.

On December 03 2012 18:51 Arnstein wrote:
What does a <> b mean in Python?


Did they just stick this in there for people coming from php (it's not exactly the same thing in php but it's close - in php it checks for !equality after implicit type conversions), or is there another language they're both getting this syntax from?

<> Is used in both SQL and Visual basic.

And is very annoying when you've been working in these languages for so long that when you switch to something else you've forgotten other languages use != and can't figure out why your code won't compile. ~_~
twitch.tv/cratonz
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
December 03 2012 23:43 GMT
#4151
On December 03 2012 08:42 Morfildur wrote:
Show nested quote +
On December 03 2012 08:24 darkness wrote:
On December 03 2012 06:09 Fyodor wrote:
On December 03 2012 05:56 darkness wrote:
Java: Is it possible to send output across the network to every user within a linked a list? If yes, how? I've tried some google search, but I found nothing.

for each loop? I'm not sure I understand at what level your problem is.


Some users are added to a linked list. I want to deliver a msg to all of them. They're all connected. loop is ok, but how do I make sure everyone receives the message? E.g. out.println("text") doesn't ask for receiver.


That depends on what you want to send it through...


out = new PrintWriter
(new OutputStreamWriter(client.getOutputStream()));

Does that help? I'd like to send out.println(); to every user that is connected atm. No idea how to do it.
3FFA
Profile Blog Joined February 2010
United States3931 Posts
December 04 2012 00:47 GMT
#4152
Can someone explain the use of rand() and srand() in (not objective or ++)C programming to me? I'm very confused as to how I should use it properly, and my google searches on these have lead to nothing that I fully understood. The use of this will be in the rolling of a 6-sided dice 60 times so it would help to also learn how to make it only output random integers from 1-6.
"As long as it comes from a pure place and from a honest place, you know, you can write whatever you want."
heroyi
Profile Blog Joined March 2009
United States1064 Posts
Last Edited: 2012-12-04 01:13:54
December 04 2012 01:08 GMT
#4153
On December 04 2012 09:47 3FFA wrote:
Can someone explain the use of rand() and srand() in (not objective or ++)C programming to me? I'm very confused as to how I should use it properly, and my google searches on these have lead to nothing that I fully understood. The use of this will be in the rolling of a 6-sided dice 60 times so it would help to also learn how to make it only output random integers from 1-6.

if i am not mistaken:

rand() is a pseudo random generator i.e it follows a "formula" to create random number thus the pseudo. Usually the basic formula consists of simple addition, multiplication and shifts.

srand is used to tell where in the list of the "random" numbers to start from in its VERY long list of numbers (for example: tell it to start the "random" generator at the 7th number)

wat wat in my pants
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2012-12-04 02:20:15
December 04 2012 02:19 GMT
#4154
On December 04 2012 09:47 3FFA wrote:
Can someone explain the use of rand() and srand() in (not objective or ++)C programming to me? I'm very confused as to how I should use it properly, and my google searches on these have lead to nothing that I fully understood. The use of this will be in the rolling of a 6-sided dice 60 times so it would help to also learn how to make it only output random integers from 1-6.

Yeah, there's a huge random number table that C uses to pull numbers from (compiler specific). Rand pulls a number from this table and increments the current table pointer. srand seeds the pointer to this table, which means it sets the index pointer to a randomized location, a very randomized location. So in order to generate "random" seeds your program needs a clever way of seeding the PRNG (psuedo-random number generator) on program start.

http://www.randygaul.net/2010/11/02/prngs-pseudo-random-number-generator/
Recognizable
Profile Blog Joined December 2011
Netherlands1552 Posts
December 04 2012 11:04 GMT
#4155
On December 04 2012 06:48 Blisse wrote:
Show nested quote +
On December 04 2012 06:26 Recognizable wrote:
So many ways to write something, I'm kinda confused. On another note, can anyone recommend a good book for learning to program?


vowels=["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
original = raw_input('Enter a word:').lower()
if len(original) > 0 and original.isalpha():
if original[0]in vowels:
print original()+"ay"
else:
print original[1:]+original[0]+"ay"
else:
print 'empty'


This is what I ended up with, anyway I have to go sleep.



There are always a lot of ways to write something; there is no "best way." It always depends on what you want to achieve with your code.

However, there are a lot of "bad ways."

One such way is writing code with lots of redundancies and it's always frowned upon.

For example, if you call lower() on a string, then you check if the string has any uppercase letters. It even sounds redundant, no? as how can a lower cased string have uppercase letters anyways?

Using len(word) > 0 is redundant if you also have a isalpha() check, since isalpha fails on empty strings. (I think isspace() is unnecessary though and I'm not sure if isalpha() fails for sentences with spaces in them, as our assumption is this program will only be fed words and not strings.)

So those are some good practices.

Other good practices is also making use of special functions inside the programming language you're working on; in this example, "element in list", instead of writing your own ways to check it, like where you used "first_letter == ... ".


In Python, the favourite practice is usually make your code as verbose and succinct as possible. Make it highly readable, but also very short and sweet. If I read through any part of your code, it should be obvious what you're doing, but also that your way of doing that is the fastest and most maintainable way to do it.

When we reversed your if...else position, we did it because now it's perfectly clear when you handle your errors and what you determined are errors. They're in the same block of code.


The best way to learn is actually just to code a lot a lot in the language, write lots of mini-programs, keep up-to-date knowledge of the language, and look at good stacks exchange solutions to your problems and why they're good.

If you want to learn about programming in general, scope, exceptions and such, there are some books, but best practices come from just writing a lot of code, and debugging your horrible code when it doesn't work.


tl;dr It's a lot more about understanding why you would use one piece of code instead of an another.


I understand the redundancies, silly of me. What do you mean by element in list? IF, ELSE, makes sense. Thanks. I already have an idea for something I'd like to program. But first I am going to finish the python codeacademy courses before I start.
Kambing
Profile Joined May 2010
United States1176 Posts
December 04 2012 13:06 GMT
#4156
On December 04 2012 20:04 Recognizable wrote:
Show nested quote +
On December 04 2012 06:48 Blisse wrote:
On December 04 2012 06:26 Recognizable wrote:
So many ways to write something, I'm kinda confused. On another note, can anyone recommend a good book for learning to program?


vowels=["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
original = raw_input('Enter a word:').lower()
if len(original) > 0 and original.isalpha():
if original[0]in vowels:
print original()+"ay"
else:
print original[1:]+original[0]+"ay"
else:
print 'empty'


This is what I ended up with, anyway I have to go sleep.



There are always a lot of ways to write something; there is no "best way." It always depends on what you want to achieve with your code.

However, there are a lot of "bad ways."

One such way is writing code with lots of redundancies and it's always frowned upon.

For example, if you call lower() on a string, then you check if the string has any uppercase letters. It even sounds redundant, no? as how can a lower cased string have uppercase letters anyways?

Using len(word) > 0 is redundant if you also have a isalpha() check, since isalpha fails on empty strings. (I think isspace() is unnecessary though and I'm not sure if isalpha() fails for sentences with spaces in them, as our assumption is this program will only be fed words and not strings.)

So those are some good practices.

Other good practices is also making use of special functions inside the programming language you're working on; in this example, "element in list", instead of writing your own ways to check it, like where you used "first_letter == ... ".


In Python, the favourite practice is usually make your code as verbose and succinct as possible. Make it highly readable, but also very short and sweet. If I read through any part of your code, it should be obvious what you're doing, but also that your way of doing that is the fastest and most maintainable way to do it.

When we reversed your if...else position, we did it because now it's perfectly clear when you handle your errors and what you determined are errors. They're in the same block of code.


The best way to learn is actually just to code a lot a lot in the language, write lots of mini-programs, keep up-to-date knowledge of the language, and look at good stacks exchange solutions to your problems and why they're good.

If you want to learn about programming in general, scope, exceptions and such, there are some books, but best practices come from just writing a lot of code, and debugging your horrible code when it doesn't work.


tl;dr It's a lot more about understanding why you would use one piece of code instead of an another.


I understand the redundancies, silly of me. What do you mean by element in list? IF, ELSE, makes sense. Thanks. I already have an idea for something I'd like to program. But first I am going to finish the python codeacademy courses before I start.


He means what you proposed above with


original[0] in ['a', 'e', 'i', 'o', 'u']


Using list inclusion is an more concise and idiomatic way (i.e., what Python experts would do) to express an equality check among several options. For example, these two pieces of code are equivalent:


if response.lower() == 'yes' or response.lower() == ' no':


and


if response.lower() in ['yes', 'no']:


But the second is preferred in Python.
Recognizable
Profile Blog Joined December 2011
Netherlands1552 Posts
December 04 2012 16:26 GMT
#4157
Ok, I figured it was that, I just wanted a more elaborate explanation. Thank you. I have almost zero knowledge of programming terminology. I'll slowly learn I guess.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2012-12-05 00:33:18
December 05 2012 00:30 GMT
#4158
What do you think guys, is newCachedThreadPool() very efficient to use?

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool()

I know what description says, but has anyone actually felt the difference?
endy
Profile Blog Joined May 2009
Switzerland8970 Posts
Last Edited: 2012-12-05 08:11:00
December 05 2012 04:57 GMT
#4159
Hey guys,

I'm very rusty with Java programming, but I cannot figure out my issue. I wrote a class that connects to a relational database and because I can potentially use various JDBC drivers, I went for a Class.forName() rather than an import.

In my code I use :
Class.forName(this.properties.getDriver());

and this.properties.getDriver() returns "com.microsoft.sqlserver.jdbc.SQLServerDriver"

I compiled using -cp and the correct path to the sqljdbc.jar file.

During execution I get a classNotFoundException com.microsoft.sqlserver.jdbc.SQLServerDriver

I tried to use import com.microsoft.sqlserver.jdbc.SQLServerDriver; at the beginning of the code and it compiles without error, so that proves I link to sqljdbc.jar correctly during compilation right ?

Thanks


edit : I compiled with Netbeans and it works, still don't get what I did wrong earlier o_o
ॐ
ghrur
Profile Blog Joined May 2009
United States3786 Posts
December 05 2012 08:41 GMT
#4160
Hi guys,

So next semester, I will be learning Python as part of a Comp Sci class I'll be taking. At the end of the class, we'll have to make a project primarily in Python. I've heard about this from my friends who're taking it now, so I've started contemplating the project. One of the thoughts I had was making an AI of sorts to play BW. It'd probably work through BWAPI. The problem is, a lot of my friends also say Python is too slow to be able to make the calculations for a game AI.

Anyway, I'd just like some opinions on if it'd be possible to build a BW AI-bot in python.

Thanks!
darkness overpowering
Prev 1 206 207 208 209 210 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 50m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech122
CosmosSc2 57
StarCraft: Brood War
Aegong 978
Artosis 649
scan(afreeca) 37
Purpose 11
Counter-Strike
Stewie2K721
taco 410
Foxcn293
Other Games
summit1g7478
tarik_tv3691
Grubby2272
FrodaN2117
Sick355
C9.Mang0195
ViBE194
shahzam181
ToD175
Day[9].tv91
Mew2King48
PPMD31
Organizations
Other Games
BasetradeTV23
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• RyuSc2 47
• musti20045 35
• IndyKCrew
• Migwel
• sooper7s
• AfreecaTV YouTube
• intothetv
• Kozan
• LaughNgamezSOOP
StarCraft: Brood War
• Pr0nogo 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21937
League of Legends
• Doublelift4268
Counter-Strike
• Shiphtur172
Other Games
• imaqtpie1000
• Day9tv91
Upcoming Events
Replay Cast
50m
LiuLi Cup
11h 50m
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
16h 50m
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
Big Brain Bouts
16h 50m
Iba vs GgMaChine
TriGGeR vs Bunny
Reynor vs Classic
Serral vs Clem
BSL Team Wars
19h 50m
Team Hawk vs Team Dewalt
BSL Team Wars
19h 50m
Team Hawk vs Team Bonyth
Code For Giants Cup
23h 20m
SC Evo League
1d 12h
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
Maestros of the Game
1d 16h
ShoWTimE vs Cham
GuMiho vs Ryung
Zoun vs Spirit
Rogue vs MaNa
[BSL 2025] Weekly
1d 18h
[ Show More ]
SC Evo League
2 days
Maestros of the Game
2 days
SHIN vs Creator
Astrea vs Lambo
Bunny vs SKillous
HeRoMaRinE vs TriGGeR
BSL Team Wars
2 days
Team Bonyth vs Team Sziky
BSL Team Wars
2 days
Team Dewalt vs Team Sziky
Monday Night Weeklies
3 days
Replay Cast
4 days
Sparkling Tuna Cup
4 days
LiuLi Cup
5 days
Replay Cast
6 days
The PondCast
6 days
RSL Revival
6 days
Maru vs SHIN
MaNa vs MaxPax
Liquipedia Results

Completed

CSL Season 18: Qualifier 1
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
Acropolis #4 - TS1
CSL Season 18: Qualifier 2
SEL Season 2 Championship
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
Sisters' Call Cup
Skyesports Masters 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.