2

I am new and trying my hands on python. I wrote a program in python and when I execute I get errors. Can anyone please help me?

Program:

first = raw_input("Enter the first no:")
second = raw_input("Enter Second no:")
if first <= 0:
        print "Enter a valid number"
if second <= first:
        print "Sencond number should be greater than first"
for x in range(first,second):
        for i in range(2, i):
                if x % i == 0:
                        j = x/i
                        print x,  " is not a prime no"
                        print "%d = %d*%d" % (x, i, j)
                        break;
                else:
                        print x, " is not a prime number"

Input and error:

Enter the first no:1
Enter Second no:9
Traceback (most recent call last):
  File "today1.py", line 7, in <module>
    for x in range(first,second):
TypeError: range() integer end argument expected, got str.

Thank you in advance.

1
  • well, from stdin you get strings, not numbers. You'll have to convert them. (int() comes to mind) Commented Feb 7, 2014 at 8:39

1 Answer 1

6

raw_input() returns a string.

Try this code:

    first = int(raw_input("Enter the first no:"))
    second = int(raw_input("Enter Second no:"))

And are you sure, that for i in range(2, i): shouldn't be for i in range(2, x):?

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

2 Comments

Thanks Max, It worked. And yes, that would be x, not i. cheers. :)
Hey Max...I don't have that much reputation to like a post. So, I hope you understand. I am really thankful.

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.