[Tutor] Is my style OK in this elementary student exercise?

Lie Ryan lie.1296 at gmail.com
Sat Jul 4 20:14:12 CEST 2009


Angus Rodgers wrote:
> 
> Any comments? (I don't expect the Spanish Inquisition!)
> 

In addition to what others have mentioned:
- use string formatting instead of concatenation as they're usually more
readable (and faster too?):
ans = raw_input(question + ' ' + true[0] + ' or ' + false[1] + '? ')
to
ans = raw_input('%s %s or %s?' % (question, true[0], false[1]))
or in python 3.1:
ans = raw_input('{} {} or {}?'.format(question, true[0], false[1]))

(note: in python3.0, .format() only accepts explicitly numbered format
string, e.g. '{0} {1} or {2}?')



More information about the Tutor mailing list