user2891439
user2891439

Reputation: 19

Python won't properly compare my assigned variables

Help, I have no idea where I went wrong in my coding and I think that I didn't do something I should have, but everybody I asked says it's a good code. I assigned a variable (stats) to be a certain integer (15). Then I asked a user to input an integer into another variable (DEX). The program then will print one of three things depending if the variable is greater, less than, or equal to stats. Here's the full code for those who want to help:

stats = 15
DEX = raw_input(prompt)
if stats > DEX:
    os.system("cls")
    print TITLE
    print "SO YOUR DEX IS %s CORRECT?" %(DEX)
    time.sleep(4)
    thread_2()
elif DEX > stats:
    print "YOU HAVE TOO MUCH DEX!!"
elif DEX = stats:
    print "ARE YOU SURE YOU WANT TO ADD ALL YOUR STATS TO DEX?"

Upvotes: 2

Views: 93

Answers (3)

user2555451
user2555451

Reputation:

You need to make DEX an integer by putting it in int:

DEX = int(raw_input(prompt))

raw_input always returns a string object. Meaning, you are trying to compare strings and integers, which won't work.

Also, regarding your last elif, you need to use == for comparison tests. = is for variable assignment.

Upvotes: 4

Hess
Hess

Reputation: 130

You are using the assignment operator in your last elif rather than the comparison operator. Your last elif should be:

elif DEX == stats:
   print "ARE YOU SURE YOU WANT TO ADD ALL YOUR STATS TO DEX?"

Upvotes: 2

devnull
devnull

Reputation: 123608

You need to say:

DEX = int(raw_input(prompt))

raw_input reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

int converts a number or string x to an integer.

Upvotes: 1

Related Questions