0

Hello I am extremely new to python and I am trying to create a program where the end-user will input a number and my program will count to that number skipping by 2. for example:

enter a number: 10
you entered: 10
4
6
8
10

How can I count by 2 with python, I tried doing this:

number += 1

but I get

typeerror: cannot concatenate 'str' and 'int' objects

can you tell me what is wrong with my coding? this is what I have

number = raw_input ("Please enter a number:")
while number < '4':
    print raw_input("Please enter a number bigger than 4:")
number += 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • anabel anguiano, Please don't forget to check an answer as correct answer. –  Jan 03 '15 at 23:37

6 Answers6

1

Your var, number, is of the type string. You have to convert it to a 'number' like type, like float or int before you can calculate with it. To do this, wrap int() around your raw_input() call and change your while loop to check for < 4 rather than < '4'. To have it calculate as you want, using steps of two, you should use xrange. Example:

number = int(raw_input("Please enter a number:"))
while number < 4:
    number = int(raw_input("Please enter a number bigger than 4:"))

for i in xrange(4, number+1, 2):
    print i
Bjorn
  • 5,272
  • 1
  • 24
  • 35
0
n = raw_input("Please enter a number:")
n = int(n)
i = 2
while i<=n:
    print(i)
    i += 2
implicati0n
  • 269
  • 2
  • 10
0
number = raw_input ("Please enter a number:")
while number < '4':
    print raw_input("Please enter a number bigger than 4:")
number += 1

The error message says, you are trying to add 1 to a string. This is why you are getting error. You know you can't count with strings. So you should change your codes to;

x=2
while True:
    try:
        number=int(raw_input ("Please enter a number: "))
    except ValueError: #catching ValueError, if user entry something that is not number
        print("You should enter a number.")
        continue #if user entry something that is not number, then skip other codes ask again.
    if number<4:
        print("Please enter a number bigger than 4")
        continue #if user entry a number less than 4, then skip other codes ask again.
    else: #if everything is fine
        while x<=number: #until x equal to number
            print (x) #print x, if you don't want to print 2 then remove this line.
            x+=2    #and add 2 to x
        break #if second while loop is done, break the first while loop.

Output:

>>> 
Please enter a number: 3
Please enter a number bigger than 4
Please enter a number: 10
2
4
6
8
10
>>>

>>> 
Please enter a number: 2
Please enter a number bigger than 4
Please enter a number: asd
You should enter a number.
Please enter a number: 10
2
4
6
8
10
>>> 
0

Note that raw_input() has retired in 3.X.

If I understand correctly, here's your code:

number = int(raw_input("Enter a number: ")) #int() to cast the string into an integer
while number < 4:
    number = int(raw_input("Invalid number, please enter a number above 4: ")) #input validation
i = 4
while (i <= number):
    print(i)
    i+=2 #add 2 to the value of i

Let me know if I misunderstood your prompt.

Rahul Syal
  • 13
  • 3
0

You're trying to add 1 to "4". This doesn't work for the same reason you can't add 1 to "Basketball" -- it's dealing with incompatible data types. Do this first, then:

number = raw_input("Enter a number: ")
number = int(number) # cast to int
if number < 4:
    number = raw_input("Enter a number: ")
    number = int(number)

Note that this isn't the best way of validating input because you're repeating yourself, but it IS closest to what you have in your question. For better answers on that subject read this canonical question

Also it's kind of a strange way to count by two. There's a range function for that in Python, which takes an optional third argument for step.

for count in range(1, number+1, 2):
    print count
Community
  • 1
  • 1
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
-2
number = int(raw_input ("Please enter a number:"))
while number < 4:
    number = int(raw_input("Please enter a number bigger than 4:"))

for i in xrange(4, number+1, 2):
    print i
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • There's no explanation, nor does this answer fix the error of performing additions with a string and an int. – Dunes Jan 03 '15 at 23:23