![]() |
|
[Python] Help me figure out whats wrong with my code - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Help me figure out whats wrong with my code (/showthread.php?tid=5701142) |
Help me figure out whats wrong with my code - OliFlaw - Aug 22, 2017 Hello, I am still new to Python and learning. I have been trying to test my knowledge on things I have been learning and putting them to use by making programs to demonstrate what I have learnt. I have tried using the try and except but think it may be not doing what I want it to do and the while loop also: def name(): name = input("Input your name") if name == "Bob": print("Welcome back Bob") elif name == "John": print("Yo") else: print("You are not a registered user") error_msg = "Please make sure you input a correct value" print("Do you want to log in? Y/N") yesorno = input() try: while yesorno == "N": print("Just type Y when you want to log in") if yesorno == "Y": print("Okay, please log in below.") break except ValueError: print(error_msg) name() As you can see I am trying to make it so that if the user inputs "N" then it prints "Just type Y when you want to log in" so that the user can type "Y" whenever they want afterwards and it will break the loop. However when I run it the error msg does not work and no matter what you input when it asks for Y or N it skips straight to "Input your name" I hope you can see what I am trying to do and leave some recommendations below :) All responses are very much appreciated and I am sorry if I am making a newbie mistake, but I am a newbie so ;) Thank you! -Oli * - (╯°□°)╯︵ ┻━━┻ - Aug 22, 2017 Can you post it on github/in code tags so we can see spacing? * - Oliver Sykes - Aug 22, 2017 Okay, so I've never actually used Python before, but I think I have it figured out. I'm using 2.7, so I'm using raw_input instead of input, due to how it's read differently between the two or something You were checking the yesorno var, but you were never changing it. It wasn't being changed from inside the while, so it couldn't really do anything. I'm not sure why it was breaking from the loop, but anyway, here's a working version for 2.7 Code def name():* - Mobe - Aug 22, 2017 (Aug 22, 2017 07:19 PM)Oliver Sykes Wrote: Okay, so I've never actually used Python before, but I think I have it figured out. How can you know all of this if you haven't used Python before? * - EPLS - Aug 22, 2017 (Aug 22, 2017 07:19 PM)Oliver Sykes Wrote: Okay, so I've never actually used Python before, but I think I have it figured out. Well done, I'm not a Python user, but from basic programming knowledge this seems like it might be the fix. * - Oliver Sykes - Aug 22, 2017 (Aug 22, 2017 07:20 PM)Mobe Wrote: How can you know all of this if you haven't used Python before? I googled the error I had. Code Traceback (most recent call last):I googled "NameError: name is not defined" and I came across this: https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined I fixed indentation errors with common sense. Indenting at colons and such. That's what code tags are for btw. * - Vana - Aug 22, 2017 I refactored and fixed your code: PHP Code I wasn't sure what you were trying to do with `error_msg` so I omitted it. * - OliFlaw - Aug 23, 2017 Thank you to everyone who has made an effort to create a response, and also thank you for fixing the code ! I understand why it wasnt doing what I wanted to do now. Thank you all very much :) |