Here's what I'm supposed to do for an exercise in the book I'm using (no solutions...):
1-Pick any two numbers with a difference of at least 6. If the the difference exceeds six, exit the program.
2- Re-organize the numbers in ascending order and find a number between the first and second number that is divisible by 5 and print all three numbers in ascending order.
At hindsight, it seems simple. Here's my code:
import math
def main():
numberOne= int(input("Enter first number: "))
numberTwo= int(input("Enter second number: "))
if abs(numberOne-numberTwo)!= 6:
input("This program shall terminate")
input()
else:
if numberOne < numberTwo:
sortandmod(numberOne,numberTwo)
else:
b = numberOne
a = numberTwo
numberOne = a
numberTwo = b
sortandmod(numberOne,numberTwo)
def sortandmod(numberOne,numberTwo):
if numberOne%5 == 0:
n = numberOne + 1 #I STRONGLY SUSPECT THAT THE ISSUE IS TAKING PLACE HERE
i= 0
alpha = 1
while alpha != 0:
n= n + i
alpha = n%5
i = i+1
lol = n
print("lol= ",lol,"n= ",n)
print("The numbers sorted are: ", numberOne,", ",n,", ",numberTwo)
else:
n=numberOne
alpha = 1
i = 0
while alpha != 0 and n<numberTwo:
n= numberOne + i
alpha = n%5
i = i+1
lol = n
print(lol)
print("The numbers sorted are: ", numberOne,", ",lol,", ",numberTwo)
main()
The code works fine when I enter a first number that is not a multiple of five.
However, when I enter a number that is a multiple of five, I get a false number in between. I suspect that the while loop with my comment is the central issue.
When I type in 200 as my first number and 206 as my second number, I get "200, 210 and 206 as my output".
Thanks for the help.


Sign In
Create Account

Back to top









