Re: [Tutor] Beginner question
>def checkCave(chosenCave): > print('You approach the cave...') >time.sleep(2) > print('It is dark and spooky...') > time.sleep(2) >print('A large dragon jumps out in front of you! He opens his jaws and...') >print() > time.sleep(2) >friendlyCave = random.randint(1, 2) > if chosenCave == str(friendlyCave): > print('Gives you his treasure!') > else: > print('Gobbles you down in one bite!') >playAgain = 'yes' >while playAgain == 'yes' or playAgain == 'y': >displayIntro() >caveNumber = chooseCave() >checkCave(caveNumber) >print('Do you want to play again? (yes or no)') >playAgain = input() Hi, - Here we are passing the chosen integer (1 or 2) got from chooseCave() method to checkCave as arguement - When called in while loop inside checkCave the following happens; - The statements of approaching the cave and seeing the dragon are printed with a time interval of 2 secs between each - Randomly either 1 or 2 is generated by the randint() method of random module in python. - That randomly generated integer (1 0r 2) is compared with our integer input (1 or 2) - If they match dragon gives us gold. Or else - We will be eaten by dragon :) But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer expression it will tell the answer. And when you provide a number it will take it as int type. See below. >>> var = input() 1+2+3 >>> var 6 >>> var = input() 2 >>> type(var) >>> var = input() s Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: name 's' is not defined >>> var = input() So since the integer number is checked with string in Line 13, it will run into infinite loop. If you use raw_input() instead of input() you will be able to run the example. Regards, Krishnan On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney wrote: > On 12 August 2013 02:14, Karim Liateni wrote: > >> 5ÿt5ÿ6hhhyyyfrrtr >> >> eschneide...@comcast.net a écrit : >> >> >I've been learning python from the website 'inventwithpython.com', and >> I'm on a chapter that covers the following code: >> > > Just a quick note - not on the algorithm itself. If you run that in some > IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the > prints will then print at once with no delay ;') If that happens, run it > from the command line or try a different IDE. > > Jim > -- > > "If you don't know it's impossible, it's easier to do." --Neil Gaiman > "The Process is not the Picture...Reality can only be proved to be > weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup > "You cannot use logic to justify logic, so logic itself has no basis other > than faith." --Agrippa > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner question
>But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer >expression it will tell the answer. And when you provide a number it will take it as int type. See >below. Hi, Ignore my above statements if using Python 3. Sorry my bad. Had a doubt and went to the site to see the version of python used. My statement above is correct only if run in Python 2 and not in 3. Regards, Krishnan On Mon, Aug 12, 2013 at 11:01 PM, Krishnan Shankar wrote: > >def checkCave(chosenCave): > > print('You approach the cave...') > >time.sleep(2) > > print('It is dark and spooky...') > > time.sleep(2) > >print('A large dragon jumps out in front of you! He opens his jaws > and...') > >print() > > time.sleep(2) > >friendlyCave = random.randint(1, 2) > > if chosenCave == str(friendlyCave): > > print('Gives you his treasure!') > > else: > > print('Gobbles you down in one bite!') > >playAgain = 'yes' > >while playAgain == 'yes' or playAgain == 'y': > >displayIntro() > >caveNumber = chooseCave() > >checkCave(caveNumber) > >print('Do you want to play again? (yes or no)') > >playAgain = input() > > Hi, > > - Here we are passing the chosen integer (1 or 2) got from chooseCave() > method to checkCave as arguement > - When called in while loop inside checkCave the following happens; > - The statements of approaching the cave and seeing the dragon are > printed with a time interval of 2 secs between each > - Randomly either 1 or 2 is generated by the randint() method of > random module in python. > - That randomly generated integer (1 0r 2) is compared with our > integer input (1 or 2) > - If they match dragon gives us gold. Or else > - We will be eaten by dragon :) > > But in the code there is a flaw. input() will evaluate your user input. > i.e. If you give an integer expression it will tell the answer. And when > you provide a number it will take it as int type. See below. > > >>> var = input() > 1+2+3 > >>> var > 6 > >>> var = input() > 2 > >>> type(var) > > >>> var = input() > s > Traceback (most recent call last): > File "", line 1, in > File "", line 1, in > NameError: name 's' is not defined > >>> var = input() > > So since the integer number is checked with string in Line 13, it will run > into infinite loop. If you use raw_input() instead of input() you will be > able to run the example. > > Regards, > Krishnan > > > On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney wrote: > >> On 12 August 2013 02:14, Karim Liateni wrote: >> >>> 5ÿt5ÿ6hhhyyyfrrtr >>> >>> eschneide...@comcast.net a écrit : >>> >>> >I've been learning python from the website 'inventwithpython.com', and >>> I'm on a chapter that covers the following code: >>> >> >> Just a quick note - not on the algorithm itself. If you run that in some >> IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the >> prints will then print at once with no delay ;') If that happens, run it >> from the command line or try a different IDE. >> >> Jim >> -- >> >> "If you don't know it's impossible, it's easier to do." --Neil Gaiman >> "The Process is not the Picture...Reality can only be proved to be >> weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup >> "You cannot use logic to justify logic, so logic itself has no basis >> other than faith." --Agrippa >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Editing values from a dictionary
Hi Bob, >>> In [3]: print metadata["artist"] [u'The Incredible String Band'] <<< Here u' and ' is not something you can strip off as it is part of python datatype called UNICODE. Python prints a word or sentence in double or singles quotes when it is a STRING or UNICODE in interpreter. These are python datatypes. And even if there are any whitespaces in your data, For example: a = [u' The Incredible String Band '] Here there are leading and trailing spaces in the string which is inside the LIST. Do a a[0].strip() Another thing above is that your string in inside a LIST so to access the string to strip it, you need to specify the place value as i have done above. If you do the above you can get the string as you need it. And if you dont need a 'u' in front of your string simply convert it to a string with str() method like below. >>> s = u'spam' >>> >>> >>> s u'spam' >>> type(s) >>> str(s) 'spam' >>> type(str(s)) >>> Regards, Krishnan On Wed, Feb 26, 2014 at 10:39 PM, Bob Williams wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Hi List, > > I have two problems, but it's possible that one solution will suffice. > I am using a module called mutagen to extract audio metadata from > .flac files. The output of mutagen is in the form of a dictionary, so > > In [1]: import mutagen.flac > > In [2]: metadata = mutagen.flac.Open("/home/bob/music/artists/The > Incredible String Band/1967 The 5000 Spirits Or The Layers Of The > Onion/08 The Hedgehog's Song.flac") > > In [3]: print metadata["artist"] > [u'The Incredible String Band'] > > I now want to pass that string to another program, but I want to strip > off the leading [u' and the trailing ']. However, this doesn't work: > > In [4]: artistName = metadata["artist"][3:-2] > > In [5]: print artistName > [] > > I was expecting The Incredible String Band, not [] > > What am I doing wrong? Or what have I misunderstood? > > The other problem concerns the program that receives these arguments - > it complains (and stops with an error) if one the arguments is empty. > For example, the flac file may not have the date information: > > Traceback (most recent call last): > File "/home/bob/Documents/scripts/python/flac2mp3v2.py", line 81, in > subprocess.call(['lame', '--add-id3v2', > '--ignore-tag-errors', '--tt', str(metadata['title']), '--ta', > str(metadata['artist']), '--tl', str(metadata['album']), '--ty', > str(metadata['date']), '--tn', str(metadata['tracknumber']), '--tg', > str(metadata['genre']), tempName1, tempName3]) > File "/usr/lib/python2.7/site-packages/mutagen/__init__.py", line > 85, in __getitem__ > else: return self.tags[key] > File "/usr/lib/python2.7/site-packages/mutagen/_vorbis.py", line > 184, in __getitem__ > if not values: raise KeyError, key > KeyError: 'date' > > If it's possible to edit the string value that gets passed to > subprocess.call('lame'...) - see problem #1 above, would it also be > possible to define a default value if the original field is empty? > > Bob > - -- > Bob Williams > System: Linux 3.11.10-7-desktop > Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.12.2 > Uptime: 12:00pm up 13 days 20:00, 6 users, load average: 0.10, 0.19, 0.26 > -BEGIN PGP SIGNATURE- > Version: GnuPG v2.0.22 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ > > iEYEARECAAYFAlMOH9sACgkQ0Sr7eZJrmU5ufACeILRlmiXt4CgDa6ZpdTI3Npm5 > FToAn2+AcjNKGxJKU+9nE9IdsoEqlQdd > =JpdC > -END PGP SIGNATURE- > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor