SamH
SamH

Reputation: 231

Python if statements not reading correctly

So I'm working on a text based game and I have a shop where you can sell ores. I have an if statement checking if the amount of ore you want to sell is greater than the amount you have. No matter what number I type in it says I don't have enough.

copore = 100 #testing
    op = raw_input(p)
    if op.lower() == "copper":
        print"""
You have %r copper ore.
How much would you like to sell?
        """ % copore
        op = raw_input(p)
        if op.lower() > copore:
            print"""
You do not have that much copper ore.
You have %r copper ore.
            """ % copore
            menu()
        elif op.lower() <= copore:
            copore = copore - op
            gold = gold + (op * 2)

            print"""
You sell your copper ore for 2 gold each.
You now have %r copper ore.
and %r gold.
            """ % (copore, gold)
            menu()
        else:
            print"""
That is not a valid number
            """
            menu()

This could very easily be my own stupidity but I figured it couldn't hurt to get someone else to look at it. Thank you.

Upvotes: 0

Views: 282

Answers (2)

user849425
user849425

Reputation:

You need to convert the input from raw_input to an integer. Without converting it you're doing a string and integer comparison.

>>> a = raw_input("Test amount:")
Test amount:10
>>> type(a)
<type 'str'>
>>> a > 10
True
>>> a > 100
True
>>> b = int(a)
>>> b > 100
False

Upvotes: 5

minopret
minopret

Reputation: 4806

When you want a number from input text, you need to wrap it in the conversion function like int(op).

Upvotes: 0

Related Questions