Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,166 members, 7,957,330 topics. Date: Tuesday, 24 September 2024 at 11:12 AM |
Nairaland Forum / Science/Technology / Programming / Python Experts, Plz What Is Wrong With This Code (1317 Views)
5,000 Naira For Someone Who Can Reverse This Code / Is Something Wrong With This Code / Need Help With This Code (2) (3) (4)
Python Experts, Plz What Is Wrong With This Code by Nobody: 9:45pm On Apr 27, 2013 |
i had to adapt an example from one of the books i was reading about python programming ie python for absolute beginner. When i try to run it both through the python 2.7.3 ide and command prompt, it just sits der blinking the prompt, dosen't show if there is any error and it takes ages and noting happens and it tends to strain my system by running the cpu at 100% and my sytem slows down. I will try to upload the python file if itz possible but below is the source code and am using windows 7 32bits in compaq presario r3000, 1gig ram # this is a jumble word game where the player makes correct a jumbled word # the computer picks a random word and jumbles it # the player have to guess the correct word import random # create a sequence of words from which to choose the word to jumble WORDS = ("abandon", "baseball", "bizare", "thesarus", "bombardment", "buffet", "budgetary", "carte blanche", "cartoon", "murder", "father", "mother", "mummy", "casually", "daddy", "cockade", "codswallop", "coercive", "codeine", "highlander", "communication", "come", "love", "gladiator", "merlin", "subject", "formal", "humourous", "sailor", "technial", "written", "singular", "dating", "comparative", "dictionary", "contemporary", "grammar", "uncountable", "progressive", "dikachi", "loveth", "complicated", "sentence", "bring", "death", "opinions", "request", "vegetable", "soup", "suggestion", "food", "director", "online", "collocation", "register", "beard", "beam", "america", "africa", "asia", "australia", "ocean", "europe", "common", "error" # pick one word at random from the sequence, this is the word that is to be jumbled word = random.choice(WORDS) # assigning word to correct cos itz correct that the guess will be compared to to check if it is correct correct = word # create an empty jumbled word to initialise it jumble = "" # the while continues until word becomes empty while word: # the first part of the loop actually generates a random position in word depending on it's length position = random.randrange(len(word)) #so the letter word[position] is going to be extracted from word and added to jumble #the next line of the loop creates a new version of string jumble and it becomes it's old self plus word[position] jumble += word[position] #the next line in the loop creates a new version of word minus the one letter at the position {position} word = word[:position] + word[position + 1):] # starting phase of the game print\ ''' WELCOME TO WORD JUMBLE &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& You have to unscramble it to make a word (press the enter key at prompt to quit.) ''' print"\tThe jumble is: ", jumble guess = raw_input("\nYour guess? " guess = guess.lower() while (guess != correct) and (guess != "": print" sorry i.e not it. " guess = raw_input(" Your guess? " guess = guess.lower() if guess == correct: print" \n\tCONGRATS!!! you got it right this time." print\ ''' THANKS FOR PLAYING %%%%%%%%%%%%%%%%%% GAME WORKED ON BY KACHI BUT NOT THE ORIGINAL OWNER, ADAPTED FROM PYTHON FOR ABSOLUTE BEGINNERS. ''' |
Re: Python Experts, Plz What Is Wrong With This Code by Nobody: 9:47pm On Apr 27, 2013 |
don't mind the jumbled arrangement, am using my phone to post it ie y itz scattered, the running showed no error just dat it just sat der doin noting.
|
Re: Python Experts, Plz What Is Wrong With This Code by ciphoenix: 9:54pm On Apr 27, 2013 |
enclose the code in [code] brackets. makes it more readable. or post it to pastebin or ideone. |
Re: Python Experts, Plz What Is Wrong With This Code by Nobody: 8:05pm On Apr 28, 2013 |
python relies on indents, you need to indent this for it to make some sense. |
Re: Python Experts, Plz What Is Wrong With This Code by gothrones(m): 9:13pm On Apr 28, 2013 |
@c++_crazy You had a slight logical error in your first while loop. I fixed it and the game works fine. Here's the correction while word: The modified staement is in bold. |
Re: Python Experts, Plz What Is Wrong With This Code by Nobody: 8:18am On Apr 29, 2013 |
gothrones: @c++_crazythanks man and i really appreciate other contributions |
Re: Python Experts, Plz What Is Wrong With This Code by bb6xt(m): 9:27am On Apr 30, 2013 |
I believe random.randrange should return a list so from the line random.randrange(len(word)) try this instead
Am sure you can take it from. PS: am also posting this from my fone. |
Re: Python Experts, Plz What Is Wrong With This Code by gothrones(m): 7:50pm On Apr 30, 2013 |
^ No! random.randrange(x) doesn't return a list! It returns a random integer y, where 0 ≤ y < x. |
Re: Python Experts, Plz What Is Wrong With This Code by bb6xt(m): 11:06pm On Apr 30, 2013 |
gothrones: ^Yeah, my bad. Haven't actually used it before so was confusing it with range() because of the name. Sorry guys. |
(1) (Reply)
How My One Month Old Hard Disk Stopped Working / Php Error Translation And Error Fix / I Need Help In Choosing A Computer Course
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24 |