Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not sure why this works...
#1
I'm working on a project for class and I was not sure why this didn't work:

Input:
0
9
def hcf(a, b):
  
  for i in range(a):
    if a > b:
	    a -= b
    elif b > a:
    	b -= a
  print(a)

hcf(int(input()), int(input()))
Output:
0 (Answer should be 9)

I then started adding random things to my code and discovered that the following code worked:
Input:
0
9
def hcf(a, b):
  
  for i in range(a):
    if a > b:
	    a -= b
    elif b > a:
    	b -= a
  print(a or b)

hcf(int(input()), int(input()))
Output:
9

I know this is an odd question but I would like a better understanding of how that changed the code, making it work.
Reply


Messages In This Thread
Not sure why this works... - by goldtiger6 - Nov-14-2017, 08:12 PM
RE: Not sure why this works... - by Larz60+ - Nov-14-2017, 11:37 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020