Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—用于确定while循环中的条件的用户输入_Python_Python 2.7_While Loop_Infinite Loop - Fatal编程技术网

Python—用于确定while循环中的条件的用户输入

Python—用于确定while循环中的条件的用户输入,python,python-2.7,while-loop,infinite-loop,Python,Python 2.7,While Loop,Infinite Loop,为什么下面的代码会产生一个无限循环?如果我硬编码y的值等于10,它不会永远持续下去,但是如果我通过用户输入输入10,它会持续下去 x = 0 y = raw_input("Enter a Number: ") while x <= y: x = x + 1 if x %2 == 0: print x else: print "odd" x=0 y=原始输入(“输入数字:”) 当x时,您可能需要将y值(现在是字符串)转换为数字 例如

为什么下面的代码会产生一个无限循环?如果我硬编码y的值等于10,它不会永远持续下去,但是如果我通过用户输入输入10,它会持续下去

x = 0
y = raw_input("Enter a Number: ")

while x <= y:
    x = x + 1
    if x %2 == 0:
        print x
    else:
        print "odd"
x=0
y=原始输入(“输入数字:”)

当x时,您可能需要将y值(现在是字符串)转换为数字

例如:

y = int(raw_input("Enter a number: "))

在解释@SiggyF的答案时,将您的代码修改为以下内容,以消除不必要的行:

y = int(raw_input("Enter a number: "))
for x in range(0, y+1):
    if x % 2 == 0:
        print(x)
    else:
        print("odd")

使用
而x@vaultah,它并不完全是重复的。提问者根本不知道
raw\u input()
会返回字符串,而不管输入是什么。另一个重复目标:另外,我觉得他们不应该在x时使用
,而应该为类似的问题分发dunce徽章。我现在正在掌心。非常感谢。