Home
Sunday, July 23, 2017
12:46:13 AM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
Basic python problem
schplade 
20/8/08 12:42:26 PM
Guru

Hey guys doing a bit of python for uni and I'm having a few issues with the code for my assignment.

I'm having a issue with an if statement (sounds dumb I know), it seems to be always going through the if statement even if it does not seem to be valid.

Anyway here is my code

 
def punctuation_check(word):

for secondary_index in range(len(word)):

if(word[-secondary_index] == "'" or "." or "," or "!" or "?" or ":"
or ";"):
extra_bit = word[-secondary_index:]
word = word[0:-secondary_index]
return word, extra_bit

else:
extra_bit = ''

return word, extra_bit


def change_sex(string):
#Defining all variables.
male = ['OUR_HERO', 'THE_VILLAIN']
female = ['OUR_HEROINE', 'THE_VILLAINESS']
split_string = string.split()
sentence = ''
space = ' '

#Main method for parsing each word in the sentence and checking for gender
#to be changed.
for main_index in range(len(split_string)):

split_string[main_index], extra_bit = punctuation_check(
split_string[main_index])


if(split_string[main_index] == male[0]):
sentence = sentence + space + female[0] + extra_bit

elif(split_string[main_index] == male[1]):
sentence = sentence + space + female[1] + extra_bit

elif(split_string[main_index] == female[0]):
sentence = sentence + space + male[0] + extra_bit

elif(split_string[main_index] == female[1]):
sentence = sentence + space + male[1] + extra_bit

else: sentence = sentence + space + split_string[main_index] + extra_bit

#Removes space from start of sentence.
sentence = sentence[1:]

return sentence



But the issue seems to stem from the first function.

Thanks any help you can provide and I understand if any of you have issues with helping with a uni assignment.

-----

eckythump 
20/8/08 1:49:34 PM
Overlord

I'm not a python programmer, and python may not work this way, but this is the first thing I noticed.

In every other language I've used, you've always had to go:
if (var == "x" or var == "y" or var == ...)
rather than
if (var == "x" or "y" or "z" or ...)


Could that be it?

-----
"Grandfather had an accident, he got burnt." "Oh no, how bad?" "Well, they don't fuck around at the crematorium."

schplade 
20/8/08 1:52:41 PM
Guru

I did have that for a while, when the list was smaller, but in the interpreter i tired the shorter version and it seemed to work and it seems to be a nice way to neaten up my code, but I'll give it a shot again.

Thanks

EDIT: Yep that works thanks for that.



Edited by schplade: 20/8/2008 1:56:41 PM

-----

eckythump 
20/8/08 2:56:41 PM
Overlord

Welcome.

-----
"Grandfather had an accident, he got burnt." "Oh no, how bad?" "Well, they don't fuck around at the crematorium."

hetman 
20/8/08 7:38:26 PM
Hero
Titan


Yeh, Python behaves the same way as most languages in this case (i.e. in this case checks if word[-secondary_index] == "'" and then logically ORs it with the remainder of the parameters cast to bool).

However you can neaten that code up with python, this is exactly equivalent to what you need:
if word[-secondary_index] in ("'", ".", ",", "!", "?", ":", ";"):

-----
Wystarczy tego medrkowania.

freespace 
22/8/08 2:25:37 AM
Hero
Titan


hetman's can be improved by using a string rather than a tuple of strings:

if word[-secondary_index] in "'.,!?:;":

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

  1  
Forums | Programming