Jump to content

Python Help

jameshumphries47

So one of my 4 four homework tasks im finding very difficult this is it

Task 1

Write a program that converts any entered word into a code word using these rules: 

-"egg" is inserted into the mid-point of any word of even numbered length

 -good becomes "goeggod" 

-"ga" is inserted either side of the mid-point of any word of odd numbered length

 -puppy becomes "pugapgapy"

 

im unsure how i would go to adding the text to the string i  have got this so far

word= input("Please enter a word.")
length = len(word)
print(length)
if length % 2 == 0:
    length = length /2
    print(length)
    print("even")
else:
    print("Odd")
    length = length /2
    print(length)

this just finds out if its odd or even then i started to work out the mid point! im stuck can someone help please

Thanks  

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

if len(word) % 2 == 0:
	return word[:len(word)/2] + egg + word[len(word)/2+1:]
else:
	#figure this out yourself :P

 

basically python strings are very nice to work with, and slicing them is super nice. read up on substrings ;)

Here is a nice summary of how slicing in python works, also has a link to the extensive documentation

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, LukaP said:

if len(word) % 2 == 0:
	return word[:len(word)/2] + egg + word[len(word)/2+1:]
else:
	#figure this out yourself :P

 

basically python strings are very nice to work with, and slicing them is super nice. read up on substrings ;)

Here is a nice summary of how slicing in python works, also has a link to the extensive documentation

i have tried the code that you gave me and i dont want it in a function so i did it like this 

word= input("Please enter a word.")
if len(word) % 2 == 0:
    word = word[:len(word)/2] + egg + word[len(word)/2+1:]
    print("word")
else:
    print("odd")

but im getting the following error

TypeError: slice indices must be integers or None or have an __index__ method

even when i run your code and put it in a function?

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, jameshumphries47 said:

i have tried the code that you gave me and i dont want it in a function so i did it like this 


word= input("Please enter a word.")
if len(word) % 2 == 0:
    word = word[:len(word)/2] + egg + word[len(word)/2+1:]
    print("word")
else:
    print("odd")

but im getting the following error

TypeError: slice indices must be integers or None or have an __index__ method

even when i run your code and put it in a function?

 

Try " + egg + word[(len(word)/2) + 1:]"
i dont remember how python does maths priority, but it may be trying to resolve that into len/3 which would result in a double, not an int

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, LukaP said:

 

Try " + egg + word[(len(word)/2) + 1:]"
i dont remember how python does maths priority, but it may be trying to resolve that into len/3 which would result in a double, not an int

i now have it like this

word[:len(word)/2] + egg + word[(len(word)/2) + 1:]

word[:len(word)/2] + egg + word[(len(word)/2) + 1:]

and it still throws that error

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, jameshumphries47 said:

i now have it like this

word[:len(word)/2] + egg + word[(len(word)/2) + 1:]


word[:len(word)/2] + egg + word[(len(word)/2) + 1:]

and it still throws that error

ok now im cofused. try wrapping the slice indeces in the int() function or using the integer division //

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

ive tried like this, but its invalid syntax

word = word int([:len(word)/2]) + egg + word int([(len(word)/2) + 1:])

as you can tell python is not my strong point.

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

Check out my current projects: Selling site (Click Here)

If($reply == "for me to see"){

   $action = "Quote me!";

}else{

   $action = "Leave me alone!";

}

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, jameshumphries47 said:

ive tried like this, but its invalid syntax


word = word int([:len(word)/2]) + egg + word int([(len(word)/2) + 1:])

as you can tell python is not my strong point.

word[:int(len(word)/2)] would be the syntax

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

This is my solution:

 

word = input("Please enter a word:")
length = len(word)
if length % 2 == 0:
	midpoint = length // 2 
	output = word[:midpoint] + "egg" + word[midpoint:]
else:
	midpoint = int(length / 2 - 0.5)
	output = word[:midpoint] + "ga" + word[midpoint] + "ga" + word[midpoint+1:]

print(output)

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Ottermad said:

This is my solution:

 


word = input("Please enter a word:")
length = len(word)
if length % 2 == 0:
	midpoint = length // 2 
	output = word[:midpoint] + "egg" + word[midpoint:]
else:
	midpoint = int(length / 2 - 0.5)
	output = word[:midpoint] + "ga" + word[midpoint] + "ga" + word[midpoint+1:]

print(output)

 

its not nice to just give out the whole solution, doesnt help him learn. but yes, that is how one would do that. a nice oneliner would be

return word[:len(word)//2 - 1] + "egg" + word[len(word)//2:] if len(word) % 2 == 0 else word[:len(word)//2] + "ga" + word[len(word)//2:len(word)//2 + 1] + "ga" + word[len(word)//2 + 1:]

 

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

@jameshumphries47 your original code was good, you just forgot to enclose egg in quotes, as you want a string, not an object ;)

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×