0

I have a very simple script, the goal is that if someone enters a number between 40 and 49 for raw_input, the value of ageRisk should change from 0 to 0.004

age = raw_input("Enter your age: ")

ageRisk = 0

if age >= 40 and age < 50:
    ageRisk = 0.004

print ageRisk

However when I run this script entering 44 for the raw_input, the value for ageRisk remains at 0. Why is this?

1
  • You should switch from Python 2.7 to Python 3. Commented Mar 7, 2020 at 23:54

4 Answers 4

2

This is because the user's input is a string. To fix this, change your line age = raw_input("Enter your age: ") into age = int(raw_input("Enter your age: "))

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

Comments

2

Try changing this:

age = raw_input("Enter your age: ")

to:

age = int(raw_input("Enter your age: "))

The default for input is to treat everything as a string, and if not converted your logical does not see the numerical value it needs to reassign the values.

Comments

2

Your raw_input is taking in a number as a string. to resolve this, convert the input into an integer.

age = int(input("Enter your age: "))

Comments

1

In Python 2, raw_input() returns a string, not an integer.

You need to wrap your raw_input() in an int() call to convert it.

int(raw_input()) takes user input and returns an integer (if one was entered).

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.