简体   繁体   中英

I keep getting TypeError: 'str' object cannot be interpreted as an integer when i try to run my code

I'm almost positive that this is how I should write it but the error keeps coming up, any help? Sorry if the question is messy this is my first time asking. the premise of the program is for each player to draw 5 coins and capture eachothers coins based off of what the other person has '''

from random import randint

def game(army1, army2):
    prisoner1=""
    prisoner2=""
    print ("Player 1:",army1,"\t",prisoner1)
    print ("Player 2:",army2,"\t",prisoner2)
    print ("\n")
    while(len(army1) and len(army2)):
        if(army1[0]=='H' and army2[0]=='T'):
            prisoner1="TH"+prisoner2+prisoner1
            prisoner2=""
            army1=army1[1:]
            army2=army2[1:]
        elif (army1[0]=='T' and army2[0]=='H'):
            prisoner2="TH"+prisoner1+prisoner2
            prisoner1=""
            army1=army1[1:]
            army2=army2[1:]
        else:
            prisoner1+=army1[0]
            prisoner2+=army2[0]
            army1=army1[1:]
            army2=army2[1:]
            if(len(army1)):
                prisoner1+=army1[0]
                army1=army1[1:]
            if(len(army2)):
                prisoner2+=army2[0]
                army2=army2[1:]

        print ("Player 1:",army1,"\t",prisoner1)
        print ("Player 2:",army2,"\t",prisoner2)
        print ("\n")
    if(len(army1) and len(army2)==0):
        print ("Player 1 wins.")
    elif(len(army1)==0 and len(army2)):
        print ("Player 2 wins.")
    else:
        if(prisoner1.count('H')>prisoner2.count('H')):
            print ("Player 1 wins.")
        elif(prisoner1.count('H')<prisoner2.count('H')):
            print ("Player 2 wins.")
        else:
            print ("Tie.")


def createRandom(army):
    opp=""
    for i in range(len(army)):
        opp+="HT"[randint(0,1)]
    return opp

total=input("Enter number of games to be played :")
for i in range(total):
    print ("\nGame :",i+1)
    seq=raw_input("Enter army sequence :")
    # Change below line to :
    # game(seq,createRandom(seq))
    # if you want to randomly generate opposite army
    game(seq[:5],seq[5:])

'''

The Python range() function requires an integer value.

But the input() function returns a string value and thus total refers to a string value.

We need to convert the string to an integer.

This will fix the problem:

total = int(input("Enter number of games to be played :"))

For more information on the input() function, see the Python documentation: https://docs.python.org/3/library/functions.html#input

Specifically:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM