[Tutor] Need help solving this problem
I have been teaching myself Python using a book. The chapter I am on currently, covers branching, while loops and program planning. I am stuck on on of the challenges at the end of this chapter, and I was hoping to get some help with this. Here it is: Write a program that flips a coin 100 times and the tells you the number of heads and tails. I have tried to think about several ways to go about doing this but I have hit a wall. Even though I understand the general concept of branching and looping, I have been having trouble writing a program with these. I look forward to your reply that will help me understand these structures better. Sincerely, Raj ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need a solution.
I have been having some difficulty modifying this program to where a player has limited number of guesses and if he fails to guess correctly within that number he is asked to "Try Again" The original program code and the code that I modified are given below. Thanks for your help. Sincerely, Raj The Original program code: # Guess my number # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" # set the initial values the_number = random.randrange(100)+1 guess = int(raw_input("Take a guess:")) tries = 1 #guessing loop while guess != the_number: if guess > the_number: print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess:")) tries += 1 print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input("\n\nPress the enter key to exit") My modifications that don't quite work right in the code: # Guess my number (Modified for Chapter 3 Challenge 3) # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money # The player has only Five guesses in which to guess correctly # otherwise he loses and is asked to try again # and the game ends import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" # set the initial values the_number = random.randrange(100)+1 guess = int(raw_input("Take a guess:")) tries = 1 #guessing loop while guess != the_number: if guess > the_number: print "Lower..." else: print "Higher..." while tries < 5: if guess == the_number: print "You're right, the number is",the_number else: print "try again" guess = int(raw_input("Take a guess:")) tries += 1 raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need a Solution!
This email contains another modified code that I tried working with but still doesn't do the job right, although performs closer to what i want. I have been having some difficulty modifying this program to where a player has limited number of guesses and if he fails to guess correctly within that number he is asked to "Try Again" The original program code and the code that I modified are given below. Thanks for your help. Sincerely, Raj The Original program code: # Guess my number # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" # set the initial values the_number = random.randrange(100)+1 guess = int(raw_input("Take a guess:")) tries = 1 #guessing loop while guess != the_number: if guess > the_number: print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess:")) tries += 1 print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input("\n\nPress the enter key to exit") My modifications that don't quite work right in the code: # Guess my number (Modified for Chapter 3 Challenge 3) # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money # The player has only Five guesses in which to guess correctly # otherwise he loses and is asked to try again # and the game ends import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" # set the initial values the_number = random.randrange(100)+1 guess = int(raw_input("Take a guess:")) tries = 1 #guessing loop while guess != the_number: if guess > the_number: print "Lower..." else: print "Higher..." while tries < 5: if guess == the_number: print "You're right, the number is",the_number else: print "try again" guess = int(raw_input("Take a guess:")) tries += 1 raw_input("\n\nPress the enter key to exit") Modified Code #2 (Better than the last one but still not right) # Guess my number (Modified for Chapter 3 Challenge 3) # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money # The player has only Five guesses in which to guess correctly # otherwise he loses and is asked to try again # and the game ends import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" # set the initial values the_number = random.randrange(100)+1 guess = int(raw_input("Take a guess:")) tries = 1 #guessing loop while guess != the_number and tries <5: if guess > the_number: print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess:")) tries += 1 if tries > 5: guess == the_number print "You're right" else: print "You lose" raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help Needed
I am looking to build a program that gets a message from the user and then prints it backward. I 've been at it for hours now but I can't seem to figure it out. I've been having trouble trying to index the message so I can print it out backwards. I would've posted my code but I really haven't gotten past the 'raw_input("Enter a message: ")' command line. Any help is gladly appreciated. Thanks! -Raj ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Needed
I had previously emailed y'all regarding inverting a message input by the user of the program backwards. After much contemplation on your earlier replies I came up with the code I have included in this email. The problem I am having with this code is that the the first character of the message that is reversed does not come up. Is there a solution to this? For my message that I input I used "take this" to test it, use the same message when the program prompts you to enter the message and run it, you'll see what I mean. Also is there a way to say reverse the string in a way so the reversed string would result to "this take" if you use my example? And is there a way to stop the loop without the use of break? Thanks for the help! Peace, Raj My Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[begin:end:-1] break raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
So I figured out the solution to the missing letter and I will post my code here. But I still need help figuring out the other stuff (please see my original message included in this email)! Thanks for putting up with me. Python is slowly but surely coming to me! I am psyched since this is the first programming language that I am learning! Thanks all for the assistance! -Raj New Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[::-1] break raw_input("\n\nPress the enter key to exit") My Original message: I had previously emailed y'all regarding inverting a message input by the user of the program backwards. After much contemplation on your earlier replies I came up with the code I have included in this email. The problem I am having with this code is that the the first character of the message that is reversed does not come up. Is there a solution to this? For my message that I input I used "take this" to test it, use the same message when the program prompts you to enter the message and run it, you'll see what I mean. Also is there a way to say reverse the string in a way so the reversed string would result to "this take" if you use my example? And is there a way to stop the loop without the use of break? Thanks for the help! Peace, Raj My Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[begin:end:-1] break raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need help with code.
Hi, I need help with the code below. I am trying to pair the list in the hint with the list of words. However, the list is not matching up correctly. Also when the correct guess is entered the program just continues without acknowledging the right answer. Please could y'all test this out and let me know where I am going wrong. I also want to add a scoring system where the computer rewards the person that guesses without getting a hint. Any suggestions as to how I might go about incorporating that into my code. Thanks for the help! Peace, Raj # Word Jumble V2 # Computer jumbles a randomly selected word then asks the player to guess it # If player cant guess the word he can ask for a hint # Scoring system that rewards players for solving jumble without a hint (not included in this code) import random WORDS = ("sweep","difficult", "python", "america") HINTS = ("remove dirt","not easy","computer language and a snake", "land of the free") word = random.choice(WORDS) correct = word jumble = "" while word: position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position+1):] for word in WORDS: HINTS[position] == WORDS[position] print\ """ Welcome to Word Jumble Unscramble the letters to make a word Remember Hints lower your score (Press the enter key at the prompt to exit) """ print "The jumble is: ",jumble guess = raw_input("Guess the word: ") guess = guess.lower() tries = 1 while guess != word and word !="" and tries <5: print "Sorry! That's not it!" guess = raw_input("Guess the word: ") guess = guess.lower() tries +=1 if guess == correct: print "You're right, the word is", word else: print "Maybe you need a hint:" print HINTS[position] guess = raw_input("Guess the word: ") guess = guess.lower() raw_input("\n\nPress the enter key to exit:") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using insert method on a list matrix
I would like to know how I could use the insert method in a List matrix eg. for the matrix below M=[[1,2,3], [3,2,1], [4,3,2]] if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M using the built in method insert How would I go about doing this? I've tried may List Comprehension possibilities but none of then worked. Below are some of the things I tried. >>> M=[[1,2,3], [3,2,1], [4,3,2]] >>> M.insert([1][1], 'pod') Traceback (most recent call last): File "", line 1, in M.insert([1][1], 'pod') IndexError: list index out of range >>> [row[0] for row in M.insert(1, 'pod')] Traceback (most recent call last): File "", line 1, in [row[0] for row in M.insert(1, 'pod')] TypeError: 'NoneType' object is not iterable >>> M.insert(1,'pod') >>> M [[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]] >>> M.insert[0](1,'pod') Traceback (most recent call last): File "", line 1, in M.insert[0](1,'pod') TypeError: 'builtin_function_or_method' object is unsubscriptable Any help is greatly appreciated! Thanks! -Raj ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using insert method on a list matrix
Thanks that works! But what if I wanted to modify 2 of the three lists in 2 different positions? Could you please let me know how I would go about doing that? Thanks! -Raj From: Ken Oliver To: Raj Medhekar ; Python Tutor Sent: Sunday, July 19, 2009 8:16:11 PM Subject: Re: [Tutor] Using insert method on a list matrix try M[0].insert(0, 'pod') -Original Message- > >From: Raj Medhekar > >Sent: Jul 19, 2009 7:12 PM > >To: Python Tutor > >Subject: [Tutor] Using insert method on a list matrix > > > > >I would like to know how I could use the insert method in a List matrix eg. >for the matrix below > >M=[[1,2,3], [3,2,1], [4,3,2]] > >if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M >using the built in method insert How would I go about doing this? I've tried >may List Comprehension possibilities but none of then worked. Below are some >of the things I tried. > >>>> M=[[1,2,3], [3,2,1], [4,3,2]] >>>> M.insert([1][1], 'pod') > >Traceback (most recent call last): > File "", line 1, in >M.insert([1][1], 'pod') >IndexError: list index out of range >>>> [row[0] for row in M.insert(1, 'pod')] > >Traceback (most recent call last): > File > "", line 1, in >[row[0] for row in M.insert(1, 'pod')] >TypeError: 'NoneType' object is not iterable >>>> M.insert(1,'pod') >>>> M >[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]] >>>> M.insert[0](1,'pod') > >Traceback (most recent call last): > File "", line 1, in >M.insert[0](1,'pod') >TypeError: 'builtin_function_or_method' object is unsubscriptable > > >Any help is greatly appreciated! Thanks! > >-Raj > > . ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Web crawling!
Does anyone know a good webcrawler that could be used in tandem with the Beautiful soup parser to parse out specific elements from news sites like BBC and CNN? Thanks! -Raj ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor