【发布时间】:2017-03-22 00:23:25
【问题描述】:
所以,在编程方面,我是一个完全的菜鸟,周末才开始学习 python。无论如何,作为 Youtube 教程之一的挑战,我想编写一个程序,可以找到定义范围内的所有数字,当除以 4 时会给我 0 提醒,然后它们将被列出。所以我更进一步,下面是我到目前为止所得到的。
# Program that let's you find all numbers in any range defined by user
# that can be divided without any reminder by the number also defined by user
print('Find all numbers in range...')
while True:
try:
x = int(input('From: ')) # begining of the range
y = int(input('.. to: ')) # end of the range
z = int(input('.. that can be divided by: ')) # value of the number to divide by
except ValueError: # in case of a nonint input
print('You should enter a valid number!')
continue
for a in range(x, y):
try:
if a % z == 0:
print(a, [a / z], end = ' ')
except ZeroDivisionError: # issue with implementing this exception
pass
我不使用 pass 语句,而是尝试这样做
print('You can\'t divide by 0!!') # or similar
它打印字符串 (x, y) 次。有什么建议么?非常感谢提前
【问题讨论】:
-
我已经尝试了您建议的方案并且它工作正常,但其他方式似乎没有做......没有,程序以代码 0 终止,没有返回任何值......
标签: python pycharm divide-by-zero