我正在尝试在我的else语句上附加一个循环

时间:2018-12-14 18:48:34

标签: 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 loop to work!

else:
    for ctr in range(1, inc + 1, 1):
        print(ctr)

2 个答案:

答案 0 :(得分:0)

您需要正确键入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)

答案 1 :(得分:0)

input()函数返回一个字符串;您需要将其强制转换为整数。试试这个:

if(!empty($Links_Array)) {
    $count=0;
    foreach($Links_Array as $element) {
          $html = file_get_html($element);
          $array2[$count] = array('title' => $value->find('.item_subject') );
          $count++;
    }
}