0

I'm taking a python course on codecademy at the moment and decided to try run a simple program on my computer from the terminal.

I created a basic program based on simple if, elif, else statements, really simple code as I'm trying to reinforce the basics that I'm learning, the goal is to form a response, if you have more than 49 credits, you get a congratulatory response and so forth...

firstName = raw_input("Enter your first name: ")
lastName = raw_input("Enter your last name: ")
excellenceCredits = raw_input("Enter the amount of excellence credits 
you have so far: ")

if excellenceCredits > 49 and len(firstName) >= 10:
    print "Well done " + firstName + " " + lastName + " on the 
excellence endorsement, feel proud! You also have an impressive long 
name!"
elif excellenceCredits > 49 and len(firstName) < 10:
    print "Well done " + firstName + " " + lastName + " on the 
excellence endorsement, feel proud!"
elif excellenceCredits < 50 and excellenceCredits > 40:
    print "So close " + firstName + ", better luck next time, I bet 
the " + lastName + "s are so proud of you!"
else:
    print "Keep working hard, you never know what's around the corner..."

The problem is whenever I run the program from the terminal and enter an excellenceCredits value less than 50, it still prints the wrong response, this is probably really simple, but I just can't see whats wrong with the code.

Toby Meyrick
  • 3
  • 1
  • 3

3 Answers3

2

raw_input parses the user's input as a str type, not an int.

Try:

int(raw_input("Enter the amount of excellence credits you have so far: "))

to get closer to your desired behavior.

caffreyd
  • 1,151
  • 1
  • 17
  • 25
  • Thanks heaps, funny how a little thing like this can get you scratching your head, if only codecademy explained how raw_input works better, obviously I shall have to dig deeper in my studies, but nevertheless thank you for saving me some time. – Toby Meyrick Apr 14 '17 at 04:29
0

It looks like you're comparing integers (49, 50, etc.) to the string value of excellenceCredits. This answer has more details.

To compare your raw_input as an integer, convert it by passing it to the built-in function int:

excellenceCredits = int(raw_input("Enter the amount..."))
Community
  • 1
  • 1
Ben Delaney
  • 1,082
  • 11
  • 19
  • 1
    As an aside, if you're just starting out with Python, you might want to look into using Python 3 instead of Python 2 There are more details on the differences [here, on the Python Wiki](https://wiki.python.org/moin/Python2orPython3). – Ben Delaney Apr 14 '17 at 04:27
  • Thank you, I see where I went wrong, didn't realise raw_input was only for strings and not numbers, can you use input or is that not accepted as a viable solution? – Toby Meyrick Apr 14 '17 at 04:30
  • 1
    @TobyMeyrick: Don't use Py2's `input`. It's swatting flies with a shotgun, because it includes an implicit `eval`, which can execute arbitrary code (e.g. the user enters `__import__("shutil").rmtree("/")` or whatever, and now you're deleting any files where you have write access for the parent directory). When you want an `int`, convert to `int`, don't allow arbitrary code to run. – ShadowRanger Apr 14 '17 at 04:33
0

You are going on a good way, just need to take care when the raw_input you need is of int datatype. You need to do the following when its datatype is int.

excellenceCredits = int(raw_input("Enter the amount of excellence credits 
you have so far: "))

Python 2.x There are two functions to get user input, called input and raw_input. The difference is, raw_input doesn't evaluate the data and returns as it in string form. But, input will evaluate whatever you entered and the result of evaluation will be returned.

raw_input gives you a string you must convert to an integer or float before making any numeric comparison.

Python 3.x

Python 3.x's input and Python 2.x's raw_input are similar and raw_input is not available in Python 3.x.

Surajano
  • 2,688
  • 13
  • 25