Re: Problem with the "for" loop syntax

2013-06-19 Thread Arturo B
Mmmm

Ok guys, thank you

I'm really sure that isn't a weird character, it is a space.

My Python version is 3.3.2, I've runed this code in Python 2.7.5, but it stills 
the same.

I've done what you said but it doesn't work.

Please Check it again here is better explained:

http://snipplr.com/view/71581/hangman/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with the "for" loop syntax

2013-06-19 Thread Arturo B
Sorry, I'm new in here

So, if you want to see the complete code I've fixed it:
http://www.smipple.net/snippet/a7xrturo/Hangman%21%20%3A%29

And here is the part of code that doesn't work:


#The error is marked in the whitespace between letter and in
 
def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
print (HANGMANPICS[len(missedLetters)])
print()
   
print('Missed letters: ', end='')

#Starts problem

 for letter in missedLetters:

#Finishes problem

print(letter, end=' ')
print()

blanks = '_' * len(secretWord)
 
for i in range(len(secretWord)):
  if secretWord[i] in correctLetters:
  blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
  
for letter in blanks:
print(letter, end=' ')
print()
   


When I press F5 (Run) I get a window that says: 'Invalid Syntax' and the 
whitespace between letter and in is in color red 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with the "for" loop syntax

2013-06-19 Thread Arturo B
Fixed, the problem was in 
HANGMANPICS 

I didn't open the brackets.

Thank you guys :)
-- 
http://mail.python.org/mailman/listinfo/python-list


How is this evaluated

2013-07-04 Thread Arturo B
I'm making this exercise: (Python 3.3)

Write a function translate() that will translate a text into "rövarspråket" 
(Swedish for "robber's language"). That is, double every consonant and place an 
occurrence of "o" in between. For example, translate("this is fun") should 
return the string "tothohisos isos fofunon".

So I tried to solved it, but I couldn't, so I found many answers, but I 
selected this:

def translate(s):
  consonants = 'bcdfghjklmnpqrstvwxz'
  return ''.join(l + 'o' + l if l in consonants else l for l in s)

print(translate('hello code solver'))


OUTPUT:
'hohelollolo cocodode sosololvoveror'

__
So I want to question:
How is the 

if 'h' in consonants else 'h' for 'h' in s

part evaluated? (step by step please :P )

''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)

Thank you guys
-- 
http://mail.python.org/mailman/listinfo/python-list


Bug asking for input number

2013-11-15 Thread Arturo B
Hi! I hope you can help me.

I'm writting a simple piece of code.
I need to keep asking for a number until it has all this specifications:

- It is a number
- It's lenght is 3
- The hundred's digit differs from the one's digit by at least two

My problem is that I enter a valid number like: 123, 321, 159, 346... and it 
keeps asking for a valid number.

Here's mi code:

res = input('Give me a number --> ')
hundreds = int(res[0])
ones = int(res[2])

# checks if the user enters a valid number
while not res.isdigit() or not len(res) == 3 or abs(hundreds - ones) <= 2:
res = input('Enter a valid number --> ')

Thanks for help!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bug asking for input number

2013-11-15 Thread Arturo B
MRAB your solution is good thank you I will use it.

Terry Eddy I saw my mistake about for example 2 <= 2, I think it's easier to 
use break in this case thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


How is this list comprehension evaluated?

2013-09-16 Thread Arturo B
Hello, I'm making Python mini-projects and now I'm making a Latin Square

(Latin Square: http://en.wikipedia.org/wiki/Latin_square)

So, I started watching example code and I found this question on Stackoverflow: 

http://stackoverflow.com/questions/5313900/generating-cyclic-permutations-reduced-latin-squares-in-python

It uses a list comprenhension to generate the Latin Square, I'm am a newbie to 
Python, and  I've tried to figure out how this is evaluated:

a = [1, 2, 3, 4]
n = len(a)
[[a[i - j] for i in range(n)] for j in range(n)]

I don't understand how the "i" and the "j" changes.
On my way of thought it is evaluated like this:

[[a[0 - 0] for 0 in range(4)] for 0 in range(4)]
[[a[1 - 1] for 1 in range(4)] for 1 in range(4)]
[[a[2 - 2] for 2 in range(4)] for 2 in range(4)]
[[a[3 - 3] for 3 in range(4)] for 3 in range(4)]

But I think I'm wrong... So, could you explain me as above? It would help me a 
lot.

Thanks for reading!
-- 
https://mail.python.org/mailman/listinfo/python-list


Understanding how is a function evaluated using recursion

2013-09-25 Thread Arturo B
Hi, I'm doing Python exercises and I need to write a function to flat nested 
lists
as this one: 

[[1,2,3],4,5,[6,[7,8]]]

To the result:

[1,2,3,4,5,6,7,8]

So I searched for example code and I found this one that uses recursion (that I 
don't understand):

def flatten(l):
ret = []
for i in l:
if isinstance(i, list) or isinstance(i, tuple):
ret.extend(flatten(i)) #How is flatten(i) evaluated?
else:
ret.append(i)
return ret

So I know what recursion is, but I don't know how is 

   flatten(i)
 
evaluated, what value does it returns?

Thank you

-- 
https://mail.python.org/mailman/listinfo/python-list