0

I am currently attempting to program basic positional movement for a text adventure of mine and no matter what I do the 0.1 position if statement code never runs. I have tried editing the position variable before it is passed in as well to 0.1 but nothing occurs when the function is ran. The position 0.0 code runs fine but doesn't return the value of 0.1, instead returning 'NONE'. (I found this out via a print statement within the function and all it gave was 'NONE') Is there something I'm missing that's super obvious as I feel like there is. The bare minimum code to reproduce the error is this:

def main():  
    print('Time to test out movement.')
    Position = 0.0
    while Position != 9.9:
        Position = Movement(Position)
        
    
    print('Test concluded.')

def Movement(Position):
    if Position == 0.0:
        print('You have entered this old and decrepid dungeon in hopes of finding some kind of riches.')
        print('What would you like to do?')
        print('1-Move north to room 0.1')
        PChoice = int(input())
        if PChoice == 1:
            return 0.1
        
    if Position == 0.1:
        print('Movement successful.')
        

if __name__ == '__main__': main()
Anthony
  • 23
  • 2
  • Please provide a [mre], there's no way we could run this code and reproduce your problem. Also, be aware of the limitations of the representation of floats, look at https://stackoverflow.com/questions/588004/is-floating-point-math-broken. In short: comparing floats is often problematic. So, representing room numbers by floats is not only a strange, but also probably a bad idea... – Thierry Lathuille Dec 16 '20 at 11:48
  • If a function reaches its end without hitting a return, it will return `None`. – Klaus D. Dec 16 '20 at 11:49
  • @ThierryLathuille I did provide a minimal reproducible example, the same error occurred when I ran the code above. The same error also occurred when I attempted to use strings for the room names too. – Anthony Dec 16 '20 at 11:50
  • I ran your code, entered '1', and as expected got 'Movement successful', so I can't reproduce any problem. – Thierry Lathuille Dec 16 '20 at 11:54
  • @KlausD. That seems to have fixed the issue? Still makes me wonder why not having a return makes the print statement within not work. – Anthony Dec 16 '20 at 11:55

1 Answers1

1

I think you want to stop the program when position becomes 0.1, also your code is running an infinte loop as it does not handle the cases when Position value is not 0.0 or 0.1: Try this code:

def main():
        print('Time to test out movement.')
        Position = 0.0
        while Position != 9.9:
            Position = Movement(Position)

        print('Test concluded.')

def Movement(Position):
    if Position == 0.0:
        print('You have entered this old and decrepid dungeon in hopes of finding some kind of riches.')
        print('What would you like to do?')
        print('1-Move north to room 0.1')
        PChoice = float(input())
        #print(PChoice)
        if PChoice == 1:
            return 0.1
        else:
            return PChoice

    elif Position == 0.1:
        print('Movement successful.')
        return 9.9
    else:
        PChoice = float(input())
        return PChoice

if __name__ == '__main__':
    main()

  
rohit
  • 26
  • 3