I am trying to make a game on my own and this is my first one. In it I'm trying to add features as time goes on and one day I came up with an idea to add color to certain key words. This is what I have:
print("The Text Based Game v0.1.0.0")
import time
import sys
time.sleep(1.5)
name = input("What is your name? ")
print("%s, are you a Warlock, Titan, or Hunter? Type 'list' if you would like a discription of each class." % (name))
playerclass = input()
if playerclass == ("list"):
print ("\nWarlocks are powerful guardians. They hit hard but can't take in much in return. \n\nTitans are tanks. In the front of the line taking in bullets for others.\n\nHunters are the middle man. They are fast but besides that they don't have much else to bring to the table.\n")
time.sleep(2)
print ("Will you chose to be a Warlock, Titan, or Hunter?")
playerclass = input()
Now when the code asks you what class you would like to be, I want the words "Warlock," "Titan," and "Hunter to show up in different colors. This is what I tried to do:
print("The Text Based Game v0.2.0.0")
import time
import sys
from colorama import Fore, Back, Style
print (Style.RESET_ALL)
Warlock = (Fore.BLUE + "Warlock" + Style.RESET_ALL)
Titan = (Fore.RED + "Titan" + Style.RESET_ALL)
Hunter = (Fore.GREEN + "Hunter" + Style.RESET_ALL)
time.sleep(1.5)
name = input("What is your name? ")
print ("%s, are you a " +str(Warlock)+ ", " +str(Titan)+", or" +str(Hunter)+ "? Type 'list' if you would like a discription of each class." % (name))
playerclass = input()
if playerclass == ("list"):
print ("\nWarlocks are powerful guardians. They hit hard but can't take in much in return. \n\nTitans are tanks. In the front of the line taking in bullets for others.\n\nHunters are the middle man. They are fast but besides that they don't have much else to bring to the table.\n")
time.sleep(2)
print ("Will you chose to be a Warlock, Titan, or Hunter?")
playerclass = input()
It pulls up an error that says:
Traceback (most recent call last):
File "python", line 14, in <module>
TypeError: not all arguments converted during string formatting
I don't want to write "Fore.BLUE + "Warlock" + Style.RESET_ALL" everytime inside the quote, instead I want to have the system to call back to "Fore.BLUE + "Warlock" + Style.RESET_ALL" when I write Warlock. I think what I'm thinking should work but I'm executing it wrong...
Do take note that I'm writing all of this in Repl.it online in python 3.6.1 Here is the link for the code in Repl.it: https://repl.it/@Woah_its_boobe/Error
This problem isn't because of Colorama. The problem here is that the %
operator has higher precedence than +
, so Python is trying to add the name here:
"? Type 'list' if you would like a discription of each class." % (name)
before it combines all those strings using +
. The simple solution is to wrap the entire string expression in parentheses:
Warlock = "Warlock"
Titan = "Titan"
Hunter = "Hunter"
name = "Bode"
print(("%s, are you a " +str(Warlock)+ ", " +str(Titan)+", or" +str(Hunter)+ "? Type 'list' if you would like a discription of each class.") % name)
output
Bode, are you a Warlock, Titan, orHunter? Type 'list' if you would like a discription of each class.
However, it would be easier to read if you don't add strings together manually, and use the format
method instead of %
.
Warlock = "Warlock"
Titan = "Titan"
Hunter = "Hunter"
name = "Bode"
print("{}, are you a {}, {}. or {}?".format(name, Warlock, Titan, Hunter), end=" ")
print("Type 'list' if you would like a description of each class.")
output
Bode, are you a Warlock, Titan. or Hunter? Type 'list' if you would like a description of each class.