3

I'm writing a simple program to help generate orders for a game I'm a member of. It falls into the catergory of programmes I don't actually need. But now I've started I want it to work. It all pretty much runs smoothly but I can't figure out how to stop a type-error ocurring about half way 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 help?

edit

The error I get is;

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'
5
  • Could you post the actual error? I'm guessing you end up using a string as an integer somewhere. Commented Nov 26, 2008 at 14:21
  • boy, this is bad code... Commented Nov 27, 2008 at 12:34
  • I know, I suck, I don't care... Commented Nov 27, 2008 at 13:59
  • 1
    it's ok to suck, but you should care and try to learn. in this example i would suggest you write a function that takes a list of strings (the options), prints them to the screen, gets the input and returns the associated value. this way you wouldn't have to repeat half of the code in your example. Commented Nov 27, 2008 at 16:09
  • “To go wrong in one's own way is better than to go right in someone else's.” - Dostoevsky. Its ok user33061. Its ok. Commented Apr 15, 2019 at 18:38

2 Answers 2

13

A stacktrace would've helped, but presumably the error is:

materials = 1 + (level * 1)

‘level’ is a string, and you can't do arithmetic on strings. Python is a dynamically-typed language, but not a weakly-typed one.

level= raw_input('blah')
try:
    level= int(level)
except ValueError:
    # user put something non-numeric in, tell them off

In other parts of the program you are using input(), which will evaluate the entered string as Python, so for “1” will give you the number 1.

But! This is super-dangerous — imagine what happens if the user types “os.remove(filename)” instead of a number. Unless the user is only you and you don't care, never use input(). It will be going away in Python 3.0 (raw_input's behaviour will be renamed input).

Sign up to request clarification or add additional context in comments.

1 Comment

Of course, "level * 1" is valid if s is any sequence like a string.
0

Here is an example of a Type Error and how to fix it:

# Type Error: can only concatenate str (not "int") to str
name = "John"
age = 30
message = "My name is " + name + " and I am " + age + " years old."

# Fix:
message = "My name is " + name + " and I am " + str(age) + " years old."

In the above example, the error message says that we're trying to concatenate a string and an integer which is not possible. So, we need to convert the integer to string using str() function to fix the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.