Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String out of range error
#1
I am trying get this code for my HS class. I don't know much about python, i know the basics. We are trying to create strategy in Prisoners Dilemma. I got most of mine working, i just need this part to work, and it looks like its not. I keep getting an error about this highlighted line. ERROR name 
Error:
String index out of range
team_name = 'The name the team gives to itself' # Only 10 chars displayed.
strategy_name = 'The name the team gives to this strategy'
strategy_description = 'How does this strategy decide?'
ctest = 0
btest = 0
def move(my_history, their_history, my_score, their_score):
''' Arguments accepted: my_history, their_history are strings.
my_score, their_score are ints.

Make my move.
Returns 'c' or 'b'. 
'''
ctest = 0
btest = 0
if len(their_history) > 15:
for k in their_history:
if k == 'c':
ctest += 1
if k == 'b':
btest += 1
elif len(my_history)==0:
return 'c'
elif len(their_history) > 4:
if their_history[:4] == 'cbcb':
return 'b'
elif len(their_history) > 1:
if their_history[-1] == 'b' and their_history[-2] == 'b':
return 'b'
elif len(their_history) > 15:
endtest = 0
endtest = ctest/btest
if endtest >= 0.65: <-----------------------------
return 'b'
elif endtest <= 0.45: <-----------------------------
return 'c'
else:
return 'c'
# my_history: a string with one letter (c or b) per round that has been played with this opponent.
# their_history: a string of the same length as history, possibly empty. 
# The first round between these two players is my_history[0] and their_history[0].
# The most recent round is my_history[-1] and their_history[-1].

# Analyze my_history and their_history and/or my_score and their_score.
# Decide whether to return 'c' or 'b'.
Reply


Messages In This Thread
String index out of range - by Salah71 - Jan-17-2017, 06:37 PM
RE: String out of range error - by micseydel - Jan-18-2017, 06:58 PM
RE: String out of range error - by Salah71 - Jan-18-2017, 07:38 PM
RE: String out of range error - by micseydel - Jan-18-2017, 07:46 PM
RE: String out of range error - by Salah71 - Jan-18-2017, 09:11 PM
RE: String out of range error - by metulburr - Jan-18-2017, 08:42 PM
RE: String index out of range - by metulburr - Jan-17-2017, 07:08 PM
RE: String index out of range - by Salah71 - Jan-17-2017, 07:19 PM
RE: String index out of range - by snippsat - Jan-17-2017, 09:19 PM
RE: String index out of range - by Salah71 - Jan-18-2017, 04:15 AM
String out of range error - by Salah71 - Jan-18-2017, 04:21 AM
String Index out of range error - by Salah71 - Jan-18-2017, 05:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a String index out of range error debian77 7 2,428 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 3,328 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Python Error List Index Out of Range abhi1vaishnav 3 2,413 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  python error: bad character range \|-t at position 12 faustineaiden 0 3,743 May-28-2021, 09:38 AM
Last Post: faustineaiden
  string index out of range jade_kim 4 2,697 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  List index out of range error while accessing 2 lists in python K11 2 2,177 Sep-29-2020, 05:24 AM
Last Post: K11
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,083 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  String index out of range - help please DudleyDiccle 7 3,514 Mar-27-2020, 12:10 AM
Last Post: DudleyDiccle
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,270 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  IndexError: string index out of range ? Q_Y 5 4,797 Jul-05-2019, 07:18 PM
Last Post: noisefloor

Forum Jump:

User Panel Messages

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