def cond(a,b):
if (a>2 and 99+a<b):
return True
else:
return False
def priiiiint(a,b):
br=0
for i in range(a,b+1):
if(i%5==0 and i%7!=0):
print('number {} dividable by 5 but not by 7! '.format(i))
br+=1
return(br)
def main():
kraj=True
while (kraj):
a=int(input('nmbr AAAA: '))
b=int(input('nmbr BBBB: '))
if cond(a,b):
priiiiint(a,b)
kraj=False
else:
print('try again, b is not greater than a by 99 or more')
br=priiiiint(a,b)
print('there was total {} ...numbers'.format(br))
当我使用数字例如a = 5和b = 105时,它会列出正确的结果,但它会这样做两次......我不知道为什么......帮助apretiated:)
答案 0 :(得分:0)
您正在调用prnt(a, b)
两次,因此会打印两次数字。
答案 1 :(得分:0)
您正在呼叫priiiiint(a,b)
两次,因此它会打印两次。
这就是你应该做的你喜欢的代码:
def main():
kraj=True
while (kraj):
a=int(input('nmbr AAAA: '))
b=int(input('nmbr BBBB: '))
if cond(a,b):
# Add br= here.
br=priiiiint(a,b)
kraj=False
else:
print('try again, b is not greater than a by 99 or more')
# This line prints everthing another time:
#br=priiiiint(a,b)
print('there was total {} ...numbers'.format(br))
结果:
nmbr AAAA: 5
nmbr BBBB: 105
number 5 dividable by 5 but not by 7!
number 10 dividable by 5 but not by 7!
number 15 dividable by 5 but not by 7!
number 20 dividable by 5 but not by 7!
number 25 dividable by 5 but not by 7!
number 30 dividable by 5 but not by 7!
number 40 dividable by 5 but not by 7!
number 45 dividable by 5 but not by 7!
number 50 dividable by 5 but not by 7!
number 55 dividable by 5 but not by 7!
number 60 dividable by 5 but not by 7!
number 65 dividable by 5 but not by 7!
number 75 dividable by 5 but not by 7!
number 80 dividable by 5 but not by 7!
number 85 dividable by 5 but not by 7!
number 90 dividable by 5 but not by 7!
number 95 dividable by 5 but not by 7!
number 100 dividable by 5 but not by 7!
there was total 18 ...numbers
为您提供有关代码的其他建议。
你也可以这样写下来:
def main():
while True:
a=int(input('nmbr AAAA: '))
b=int(input('nmbr BBBB: '))
if cond(a,b):
# Add br= here.
br=priiiiint(a,b)
break
else:
print('try again, b is not greater than a by 99 or more')
print('there was total {} ...numbers'.format(br))
如果你有一个True
的循环,它将始终运行,并在遇到break
时退出该循环。你的也很好,但这是一个更简单的版本,因为你需要更少的代码行,你的代码看起来更干净。