Question:
Python Question 10 POINTS?
MajorMEllow
2012-10-12 16:40:43 UTC
Hi I really need help....

I am stuck, if you can help i would really appreciate it.

So i have been given a task to make a funciton that return a string as hidden
for example:

>>> get_view ('apple'):
'^^^^^'
>>> get_view ('apple-banana')
'^^^^^-^^^^^^'

So we are only trying to hide lower-case letters and nothing else...

My func so far is:

def get_view (puzzle):
''' (str) -> str
>>>get_view ('apple')
'^^^^^'
>>>get_view ('apple-banana')
'^^^^^-^^^^^^'
'''
return len(puzzle) * HIDDEN <-- But it hides EVERYTHING...

Also, in the next function i have 3 strings...

def update_view (puzzle, view, guessed_letter):
''' (str, str, str) -> str

Return the view of the puzzle with each occurrence of the letter in
the puzzle revealed.

>>>update_view ('apple', '^^^^^', 'a')
'a^^^^'
>>>update_view ('apple', '^^^^^', 'p')
'^pp^^'

'''
view == puzzle
for i in puzzle[0:]:
if (guessed_letter in puzzle):
return guessed_letter + view[0+1:] <-- But it only reveals the first letter...i want a condition that would match the placement...

THANKS FOR ALL YOUR HELP:)
Three answers:
husoski
2012-10-14 15:00:36 UTC
We've discussed this in email, but for the record:



def hide_unguessed(word, guessed):

... """hide any letters in word that aren't in the guessed list"""

... result = ""

... for ch in word:

... ... if ch.islower() and ch not in guessed:

... ... ... result += "^"

... ... else:

... ... ... result += ch

... return result



Edit: For the benefit of Python fanatics who may be reading, here's a way to do the whole thing in one line:



return "".join( [(c if c in guessed or not c.islower() else '^') for c in word] )



That depends on three things that you probably haven't learned yet. The expression in [] brackets is a "list comprehension", which is a way of building a list using any sequence, plus a rule. The "for c in word" trailing part defines the sequence as every character in the word string. The front part is a "conditional expression" that returns c if the condition (c in guessed or not c.islower()) is true, else returns '^'. That creates a list of characters that belong in the output. Finally, the "".join(list) expression concatenates a list of strings into a single string.



I don't suggest that you go out of your way to write code like that. However, "".join() is about the fastest way to concatenate a large number of strings together. Sooner or later, it should be in your Python toolbox.
Michael
2012-10-13 13:32:00 UTC
Sounds like you want something like hangman.

The basic idea for the hide function is straightforward. Make an empty string (we'll call it RET). Next, scan through each character of the string to hide using a for loop. For each scanned character, if it's a lowercase letter, add a "hidden character" to RET. Otherwise just add whatever character shows up in the original string.



To check if a character is lower case, convert the character to an integer with the python's built-in ord() function. The lowercase letters of the alphabet correspond to integers 97-122 for a-z.

(Remember to take out the arrows if you want to run this)



def HideMe(str):

>>ret = ""

>>for i in str:

>>>>if ord(i)>=97 and ord(i)<=122:

>>>>>>ret += "^"

>>>>else:

>>>>>>ret += i

>>return ret



For the next function, it's mostly the same. But, when you go to add hidden characters, don't just check that they are lower case, also check that they are not equal to the "show" variable (as I've called it in my example), and that the "prev" variable (as I've called it) has a "hidden character" at the same location.

That way, when you do something like: func('apple','a^^^^','p') it will return 'app^^'.



def HideMeMostly(str,prev,show):

>>HideChar = "^"

>>if(len(str)!=len(prev)) return false; //prev must be a valid hidden version of str

>>ret = ""

>>for i in range(0,len(str)):

>>>>if str[i]!=show and prev[i]==HideChar and ord(i)>=97 and ord(i)<=122:

>>>>>>ret += "^"

>>>>else:

>>>>>>ret += i

>>return ret



BTW, you'll still need to add a way of checking whether the user revealed a character or not, if you are making a hangman game.

See the sources for info on ord() and range().



Good luck! :-)
nivens
2017-02-23 21:50:39 UTC
adult males: 3-4 feet women 4.5-6 feet Weight relies upon on many subject concerns, in many instances get up around 6000 + grams for a great breeding woman The set up is costly. A min 20 gal tank ( I even have all 30s), an below the tank warmth pad, (by no capacity a warmth stone, those can burn your reptile and not distribute sufficient warmth by out the tank) 2 hides, one on heat element, one on cool element, a thermometer, hydrometer, a controller to tutor on and stale your warmth pad whilst the interior reaches a undeniable temp, a great water dish sufficiently massive to soak, coconut bark or cypress bark bedding, no longer reptile carpet on the grounds that they like burrow and reptile crapets do no longer help guard humidity, and a few decorations (each and every now and then they are going to conceal at the back of those instead of their hides and you could truly see them). Mouse feeders are greater decrease priced bought in bulk, frozen. As they get greater, rats are as properly. All you do is defrost them in warm water (no longer boiling, only warm faucet and touch them to make specific they're thoroughly defrosted). The snake will understand warmth and the scent and hit it if it fairly is been consistently fed F/T. stay feeding has subject concerns, one, having to pass out and consistently get a furnish, and 2, because of the fact the animal gets greater, so does its prey. Rats can and could combat decrease back inflicting great harm on your snake. They nonetheless constrict F/T, a minimum of all of mine do. study snakes mutually with your mom. perhaps she would be in a position to get slightly much less fearful of them.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...