我正在努力使程序向前或向后计数,具体取决于start_number是小于还是大于end_number。当start_number较小时它可以工作,但如果它更大,它就不会工作。知道为什么?谢谢!
#Challenge 4.1
#Asks the user for a beginning and ending number, then asks for the amount count by. The program then prints the numbers
start_number = int(input("What number would you like me to begin with?"))
end_number = int(input("What number would you like me to end with?"))
count_number = int(input("How much would you like me to count by? " +
"(Note: for most accurate results have me count by a number " +
"that divides evenly into the number you assigned me to end with.)"))
print("\ncalculating...\n")
if start_number < end_number:
end_number += count_number
for number in range (start_number, end_number, count_number):
print(number)
elif end_number < start_number:
count_number *= -1
for number in range (end_number, start_number, count_number):
pyprint(number)
答案 0 :(得分:0)
你错误地推翻了范围的args。有了这个固定的,&#39; pyprint&#39;将是一个NameError。你也忽略了调整停止计数。尝试上下两个。
start = int(input("What number would you like me to begin with?"))
stop = int(input("What number would you like me to end with?"))
step = int(input("How much would you like me to count by?"))
if stop < start:
step *= -1
stop += step
for number in range (start, stop, step):
print(number)