Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Splits
#1
Hey guys, I'm a new student to Python 3, and I'm having some issues with an assignment, so I'd appreciate the help.
What I'm doing in the assignment is taking a user-inputted string, splitting it, and printing it out in a triangle (if it's even, inverted triangle if it's odd, but I have that part working great)
So here's where I have a problem.
if len( myString ) % 2 == 0:
    numA = ( len( myString ) / 2 )
    numB = ( numA + 1 )
    halfA = myString [ : ( numA ) ]
    halfB = myString [ ( numB ) : ]
So, the string is identified as being eve, and I'm making the attempt to use a string-split to only print half the string at a time.

When this runs, I receive an error that states:
Error:
TypeError: slice indices must be integers or None or have an __index__ method
We haven't learned about any __index__ method, and my text book, as well as online resources, aren't giving me any clues as to what I'm doing wrong.
If I NEED to use an integer in my string-split, I don't know how to do it gracefully. The only thing I can think of is to have if statements for each possible even-numbered string.

Thanks for the help guys, I appreciate it!
Reply
#2
Put below numA = ( len( myString ) / 2 ) print(type(numA)).

It seams that it is float and that can't be used as an index or into index operations like slicing.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Yep, a simple
numA = int( len(mystring)/2 )
should help.
Reply
#4
It's the python3 way. Division operation returns float.

victor@jerry:~$ python3
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 100 / 2
>>> type(a)
<class 'float'>
>>> 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  join elements from two splits dwiga 2 3,143 Sep-29-2017, 09:27 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020