Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,756 members, 7,824,167 topics. Date: Saturday, 11 May 2024 at 02:07 AM

Python Experts, Plz What Is Wrong With This Code - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Python Experts, Plz What Is Wrong With This Code (1287 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)

(1) (Reply) (Go Down)

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"wink

# 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[sadposition + 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? "wink
guess = guess.lower()
while (guess != correct) and (guess != ""wink:
print" sorry i.e not it. "
guess = raw_input(" Your guess? "wink
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 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):]



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++_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 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):]



The modified staement is in bold.
thanks 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

letters = [word[i] for i in random.randrange(len(word))]
jumble = ''.join(letters)

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: ^
No! random.randrange(x) doesn't return a list! It returns a random integer y, where 0 ≤ y < x.
Yeah, my bad. Haven't actually used it before so was confusing it with range() because of the name. Sorry guys.

(1) (Reply)

Nairalanders, Africa Needs A Computer Language / Can You Teach Me Programming In Sango-ota Area? / Mongoose/xampp: Using Php(on Mongoose) To Insert Data Into Mysql On Xampp

(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
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.