Please help my while loop does not work as expected KingKhan248 Programmer named Tim Posts: 6 Threads: 2 Joined: Sep 2020 Reputation: Sep-28-2020, 07:07 PM (This post was last modified: Sep-28-2020, 07:53 PM by Yoriz.) I was doing some math and came across a tedious problem that I thought could be solved easily by a computer, so I thought I would try writing some python code for it. I need to find the units of the integers mod m and then find the inverse of each unit. Finding the units is easy but the inverse of each unit by hand can get a bit tedious. In order to find the inverse, you're looking for a number that when multiplied by the unit, will give you 1 or -1 in the modulus. For example: in the integers mod 5, 2 and 3 are units and inverses of each other. 2 * 3 = 6 and 6%5 is 1. I thought an easy and short while loop would be able to do this just fine so I went ahead and tried it: ```unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) count = 1 x = unit%mod while x != 1: unit = unit * count count = count + 1 else: print("The inverse is: ", x)```This did not work and I'm not sure why. It does everything before the while loop just fine. I'm new to python and in my python class we just started doing while loops and I thought this would be a fun little extra curricular activity so any help is appreciated! Reply bowlofred Da Bishop Posts: 1,165 Threads: 3 Joined: Mar 2020 Reputation: Sep-28-2020, 07:12 PM (This post was last modified: Sep-28-2020, 07:48 PM by bowlofred.) Please use the bbcode python tags for your code. Among other things, we can't see the indentation properly to tell if that is correct. Also, rather than "does not work", please talk about the output you're getting (including any complete error messages) and how that compares to the output you expect. I think your code is similar to this: ```x = unit%mod while x != 1: unit = unit * count count = count + 1```Your loop depends on the value of `x`. But `x` is never modified within the loop. If you ever enter the loop, you will never exit because the conditional can never become false. Reply KingKhan248 Programmer named Tim Posts: 6 Threads: 2 Joined: Sep 2020 Reputation: Sep-28-2020, 07:29 PM Sorry about that, I should have used bbcode. When I say it does not work, I mean nothing happens. ```unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) count = 1 x = unit%mod while x != 1: unit = unit * count count = count + 1 else: print("The inverse is: ", x)```is the original code and it asks for the unit and mod and then does nothing. What I want is a while loop that takes the unit and multiplies it by 1, then does that result % mod, then checks to see that the answer is ==1. If not, it adds 1 to the count and then runs again until the the value stored in unit is equal to 1. The code has a mistake. What I wrote : ```else: print("The inverse is: ",x)```What I actually want is to print the value stored in count. Reply bowlofred Da Bishop Posts: 1,165 Threads: 3 Joined: Mar 2020 Reputation: Sep-28-2020, 07:49 PM As I wrote above, your loop condition depends on `x`, but that variable is never updated within the loop. So the while condition will never become false to exit. Reply KingKhan248 Programmer named Tim Posts: 6 Threads: 2 Joined: Sep 2020 Reputation: Sep-28-2020, 08:07 PM I edited my while loop to include the x: ```unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) count = 1 x = unit%mod while x != 1: count = count +1 unit = unit * count x = unit%mod else: print("The inverse is: ", count)```Still does not seem to exit the while loop Reply bowlofred Da Bishop Posts: 1,165 Threads: 3 Joined: Mar 2020 Reputation: Sep-28-2020, 08:35 PM (This post was last modified: Sep-28-2020, 08:36 PM by bowlofred.) It exits on some inputs, just not on the ones you expect, I guess. I think you wanted unit=2, mod=5 to exit. Look at what happens to the values each time through the loop: ``````Output:unit=2 mod=5 count=1 x=2 unit=4 mod=5 count=2 x=4 unit=12 mod=5 count=3 x=2 unit=48 mod=5 count=4 x=3 unit=240 mod=5 count=5 x=0 unit=1440 mod=5 count=6 x=0 unit=10080 mod=5 count=7 x=0 unit=80640 mod=5 count=8 x=0 unit=725760 mod=5 count=9 x=0 unit=7257600 mod=5 count=10 x=0 ...``````x will never become 1 and this loop will never exit. I haven't followed the actual algorithm you're trying to recreate, but line 7 seems suspect to me, since it overwrites the unit. Perhaps instead of line 7 changing unit, you want to calculate x as `x = (unit * count) % mod`? Reply deanhystad So-and-so of the Yard Posts: 2,025 Threads: 11 Joined: Feb 2020 Reputation: Sep-28-2020, 09:12 PM I think your logic is wrong. If the input unit is 3, the values for unit as your program loops are: 3, 6, 18, 72, 360, 2160 Don't you want them to be: 3, 6, 9, 12, 15, 18, 21? Also, you program may run forever if unit and mode are not relatively prime. For example, if unit is 3 and mod is 9, this code will print an infinite sequence: ```unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) i = 1 while (unit*i)%mod != 1: print((unit*i)%mod, end=' ') i += 1 else: print("Inverse =", i)`````Output:3 6 9 0 3 6 9 0 3 6 9 0 ...``I think the result has to be in the range 1 to mod, so I would rewrite the code like this: ```unit = int(input("Please enter the unit: ")) mod = int(input("Please enter the mod: ")) for i in range(1, mod): if (unit*i)%mod == 1: print("The inverse is: ", i) break else: print('There is no solution')``` Reply