Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 I';我试图在else语句中附加一个循环_Python_Loops - Fatal编程技术网

Python I';我试图在else语句中附加一个循环

Python I';我试图在else语句中附加一个循环,python,loops,Python,Loops,它以我希望的方式运行上半场,但它忽略了我的else语句。我试图在else语句中附加一个循环。任何建议都很好!我将在下面附上我的代码: num =input("Enter a number:") print(num) inc = input("Enter an increment:") print(inc) if inc <= str(0): print("Enter an increment greater than 0.") #works up until else--get

它以我希望的方式运行上半场,但它忽略了我的else语句。我试图在else语句中附加一个循环。任何建议都很好!我将在下面附上我的代码:

num =input("Enter a number:")
print(num)
inc = input("Enter an increment:")
print(inc)

if inc <= str(0):
     print("Enter an increment greater than 0.") #works up until else--get loop to work!

else:
    for ctr in range(1, inc + 1, 1):
        print(ctr)
num=input(“输入一个数字:”)
打印(个)
inc=输入(“输入增量:”)
印刷(公司)

如果inc您需要正确键入cast。此外,您没有在代码中的任何位置使用
num

num =int(input("Enter a number:")) #int type cast
print(num)
inc = int(input("Enter an increment:")) #int type cast
print(inc)

if inc <= 0: # remove str type cast
     print("Enter an increment greater than 0.")

else:
    for ctr in range(1, inc + 1): # no need of step argument of 1. It is 1 by default
        print(ctr)
num=int(输入(“输入一个数字”)#int类型强制转换
打印(个)
inc=int(输入(“输入增量”)#int类型强制转换
印刷(公司)
如果incinput()函数返回一个字符串;您需要将其转换为整数。试试这个:

if inc <= 0:
     print("Enter an increment greater than 0.")
else:
    for ctr in range(1, inc + 1):
        print(ctr)

if inc
str(0)
将数字0转换为字符串0。因此,在if语句中,您正在比较字符串,这是按照“词典”顺序进行的,可能根本不是您想要的。相反,使用
int
float
将inc(和num)转换为数字。是否尝试在输入中输入一个大于0的数字?是的,我尝试使任何负整数的状态为“输入大于0的增量”。但是,对于所有其他响应,让它循环通过偶数。我是否也应该在if语句下添加num?还是在我的else声明中?缺少num是它不会循环的原因吗?谢谢你的回复!!循环对我来说执行得很好。如果我给2和任何大于0的数字,谢谢!我将它们转换为整数并清理了我的循环,但循环仍然没有运行;我写了剧本,自己测试了一下。你是得到了一个异常,还是输入了一些无效的数字?在我用-1测试inc之后,我输入了11和2。这些有资格成为无效的吗?