Python type error problem
I am writing a simple program to help create orders for a game I am a member of. It falls into the category of programs that I really don't need. But now I started, I want it to work. This all pretty much runs smoothly, but I can't figure out how to stop the type error occurring about halfway through. Here's the code;
status = 1
print "[b][u]magic[/u][/b]"
while status == 1:
print " "
print "would you like to:"
print " "
print "1) add another spell"
print "2) end"
print " "
choice = input("Choose your option: ")
print " "
if choice == 1:
name = raw_input("What is the spell called?")
level = raw_input("What level of the spell are you trying to research?")
print "What tier is the spell: "
print " "
print "1) low"
print "2) mid"
print "3) high"
print " "
tier = input("Choose your option: ")
if tier == 1:
materials = 1 + (level * 1)
rp = 10 + (level * 5)
elif tier == 2:
materials = 2 + (level * 1.5)
rp = 10 + (level * 15)
elif tier == 3:
materials = 5 + (level * 2)
rp = 60 + (level * 40)
print "research ", name, "to level ", level, "--- material cost = ",
materials, "and research point cost =", rp
elif choice == 2:
status = 0
Can anyone please help?
change
The error I am getting:
Traceback (most recent call last):
File "C:\Users\Mike\Documents\python\magic orders", line 27, in <module>
materials = 1 + (level * 1)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
source to share
A stacktrace would help, but presumably the error is:
materials = 1 + (level * 1)
Level 'is a string and you cannot do arithmetic line by line. Python is a dynamically typed language, but not weakly typed.
level= raw_input('blah')
try:
level= int(level)
except ValueError:
# user put something non-numeric in, tell them off
Elsewhere in the program, you use input (), which will evaluate the input string as Python, so for "1" you get the number 1.
But! This is very dangerous - imagine what happens if the user types "os.remove (filename)" instead of a number. Unless the user is just you and you don't care, never use input (). It will go off to Python 3.0 (raw_input behavior will be renamed to input).
source to share