Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Help me figure out whats wrong with my code

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
Can you post it on github/in code tags so we can see spacing?
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():
    name = raw_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 = raw_input()
try:
    while yesorno == "N":
        print("Just type Y when you want to log in")
        if raw_input() == "Y":
            break
        print("Okay, please log in below.")
except ValueError:
    print(error_msg)
    

name()

riperino animefanerino
(Sep 12, 2016 04:11 PM){Nexus} Wrote:  If the FBI come knocking I'm gonna spew information out my ass about Aeon.

They'd have to tell me to stop giving them information.

(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.
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():
    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 = raw_input()
try:
    while yesorno == "N":
        print("Just type Y when you want to log in")
        if raw_input() == "Y":
            break
        print("Okay, please log in below.")
except ValueError:
    print(error_msg)
    

name()

How can you know all of this if you haven't used Python before?
(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.
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():
    name = raw_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 = raw_input()
try:
    while yesorno == "N":
        print("Just type Y when you want to log in")
        if raw_input() == "Y":
            break
        print("Okay, please log in below.")
except ValueError:
    print(error_msg)
    

name()

Well done, I'm not a Python user, but from basic programming knowledge this seems like it might be the fix.
(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):
  File "G:\Users\Andy\Documents\thing.py", line 13, in <module>
    yesorno = input()
  File "<string>", line 1, in <module>
NameError: name 'N' is not defined


I googled "NameError: name is not defined" and I came across this:
https://stackoverflow.com/questions/2112...ot-defined

I fixed indentation errors with common sense. Indenting at colons and such.
That's what code tags are for btw.

riperino animefanerino
(Sep 12, 2016 04:11 PM){Nexus} Wrote:  If the FBI come knocking I'm gonna spew information out my ass about Aeon.

They'd have to tell me to stop giving them information.

I refactored and fixed your code:
PHP Code

<?php 
names 
= {"Bob": "Welcome back Bob", "John": "Yo"}

while 
input("Do you want to log in? Y/N: ") != "Y":
    print(
"Just type Y when you want to log in")
print(
"Okay, please log in below.")

name = input("Input your name: ")
if 
name in names:
    print(
names[name])
else:
    print(
"You are not a registered user") 

I wasn't sure what you were trying to do with `error_msg` so I omitted it.
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 :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by theN20Dec 3, 2020 02:31 PM 9 322 Dec 3, 2020 02:31 PM
Last Post: tsuki
[Java] by CupOfJavaJan 31, 2019 04:52 PM 6 277 Jan 31, 2019 04:52 PM
Last Post: Khamul
[C] by SIM-3466371Jan 5, 2019 12:55 PM 3 166 Jan 5, 2019 12:55 PM
Last Post: HFDone