如何用 python 计算 2?

how count by 2 with python?

你好,我是 python 的新手,我正在尝试创建一个程序,最终用户将输入一个数字,我的程序将计数到该数字并跳过 2。例如:

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

如何用 python 计算 2,我试过这样做:

number += 1

但我得到

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

你能告诉我我的编码有什么问题吗? 这就是我的

number = raw_input ("Please enter a number:")
while number < '4':
    print raw_input("Please enter a number bigger than 4:")
number += 1
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

您的变量 number 的类型为 string。您必须将其转换为类似 'number' 的类型,例如 floatint 才能使用它进行计算。为此,请将 int() 包裹在 raw_input() 调用周围并更改 while 循环以检查 < 4 而不是 < '4'。要让它按你想要的方式计算,使用两步,你应该使用 xrange。示例:

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
n = raw_input("Please enter a number:")
n = int(n)
i = 2
while i<=n:
    print(i)
    i += 2
number = raw_input ("Please enter a number:")
while number < '4':
    print raw_input("Please enter a number bigger than 4:")
number += 1

错误消息说,您正在尝试将 1 添加到字符串中。这就是您收到错误的原因。你知道你不能用弦数数。因此,您应该将代码更改为;

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.

输出:

>>> 
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
>>> 

请注意 raw_input() 已于 3.X 停用。

如果我没理解错的话,这是你的代码:

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

如果我误解了您的提示,请告诉我。

您正在尝试将 1 添加到 "4"。这与您不能将 1 添加到 "Basketball" 的原因相同——它处理的是不兼容的数据类型。先做这个,然后:

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

请注意,这不是验证输入的最佳方式,因为您在重复自己,但它最接近您的问题。有关该主题的更好答案 read this canonical question

另外,以二为单位计数也是一种奇怪的方式。在 Python 中有一个 range 函数,它为 step.

采用可选的第三个参数
for count in range(1, number+1, 2):
    print count