Python Forum
Please help my while loop does not work as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help my while loop does not work as expected
#1
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
#2
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
#3
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
#4
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
#5
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
#6
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
#7
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Does Not Work Properly mactron 4 906 Jun-22-2023, 01:04 AM
Last Post: mactron
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,996 Dec-18-2021, 02:38 AM
Last Post: knight2000
  How can this for loop work without : ? Pedroski55 1 1,686 Dec-13-2020, 01:19 AM
Last Post: palladium
  For loop in my __init__ doesn't work as expected Jessy 2 2,353 Nov-18-2019, 10:07 AM
Last Post: buran
Question Why does modifying a list in a for loop not seem to work? umut3806 2 2,291 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,184 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  for loop just work one Faruk 1 2,002 Jan-19-2019, 05:34 PM
Last Post: Larz60+
  'Looping' does not work out within a 'for Loop' Placebo 4 3,326 Sep-15-2018, 08:19 PM
Last Post: Placebo
  Having issues getting a loop to work PLESSE HELP ASAP manthus007 1 2,112 Aug-25-2018, 10:44 AM
Last Post: j.crater
  Scoping Question: If else in for loop not evaluating i as expected datasundae 3 3,171 May-25-2018, 06:43 PM
Last Post: datasundae

Forum Jump:

User Panel Messages

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