Home > Software engineering >  Python simple console game
Python simple console game

Time:06-24

I've made this console application that just add 'ing' to the verbs given, but when I try to remove the letter 'e' at the end of the verbs I get this error: " 'str' object does not support item assignment "...THANKS!!!

import os

print("---What to do?---" "\n")

command=""
verbs=[1,2]

while  True:
    command=input("What should I do now? ").lower()
    if command[-1]=="e":
        command[-1]=""
    verbs.append(command)

    if command=="help":
        print("\n" "Just give me verb!" "\n")
    elif command=="rest":
        print("\n" "Fine, bye!")
        break
    elif command=="clear":
        os.system('cls')
        print("---What to do?---" "\n")
    else:

        if verbs[-1]!=verbs[-2]:
            print("\n" "Ok I'm " command "ing" "\n")
        else:
            print("\n" "I'm tired of " command "ing" "\n")

CodePudding user response:

To remove the last letter from the string just use command[:-1].

import os

print("---What to do?---" "\n")

command=""
verbs=[1,2]

while  True:
    command=input("What should I do now? ").lower()
    if command[-1]=="e":
        command = command[:-1]
        # there is one more way
        # command = list(command)
        # command[-1]=""
        # command = ''.join(command)
    verbs.append(command)

    if command=="help":
        print("\n" "Just give me verb!" "\n")
    elif command=="rest":
        print("\n" "Fine, bye!")
        break
    elif command=="clear":
        os.system('cls')
        print("---What to do?---" "\n")
    else:

        if verbs[-1]!=verbs[-2]:
            print("\n" "Ok I'm " command "ing" "\n")
        else:
            print("\n" "I'm tired of " command "ing" "\n")
  • Related