Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Python variable is always true??


  • Please log in to reply
2 replies to this topic

#1 Gotcha_Hotshot22

Gotcha_Hotshot22

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:27 PM

Posted 08 August 2014 - 06:45 PM

I don't understand why and I've tried a million different solutions but a variable defined as "Login" is always true after the user enters any kind of text. Doesn't matter what they enter it becomes true, or "yes" as my latest attempt puts it. 

 

Can anyone spot my flaw? Can python even compare text accurately?

ErrorLevel=1
Test1=False
Test2=False
setquit=False
Login="No"

while Login == "No":
    Login = "No"
    
    ErrorLevel = ErrorLevel + 1
    if ErrorLevel == 3:
        break
        
    name = input ("Username: ")
    if name == "Daniel" :
        Test1 = True;

    password = input ("Password: ")
    if password == "ABC" :
        Test2 = "Yes";

    if Test1 == Test2:
        Login = "Yes";

    if Login == "Yes":
        break
        
while Login == "Yes":    
    while setquit == False:

        print(Login)
        year = input("Current year: ")
        year = int(year)
       
        dob = input("Year of birth: ")
        dob = int(dob)
        print("You are", year - dob, "years old.")

        setquit = input("Do you want to quit? ")
        if setquit == "yes":
            quit = True; 

I realize storing the password in clear text is insecure. My next goal is hashing it somehow. 

 

Anyway, the whole age calculator thing is just practice for me.

 

EDIT: Do I have to end the loop some how so it knows when to go back to the top? I haven't seen any "loop-ending" code yet...

 

 

Thanks for all your help,

Daniel


Edited by Gotcha_Hotshot22, 08 August 2014 - 06:47 PM.

Thanks,

Daniel


BC AdBot (Login to Remove)

 


m

#2 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:27 PM

Posted 09 August 2014 - 04:09 AM

OK, so here's the first part of the problem I've seen:
        Test2 = "Yes";

If you're using Booleans, you should use Booleans, not text strings that spell something which looks to a human vaguely like a Boolean. (You could say the same for other variables such as Login, but that causing quite the same confusion.

 

The second part of the problem is

       if Test1 == Test2:

Now part of that is buggy because Test1 is a genuine Boolean (i.e. if the username was "Daniel", it will be True) unlike Test2.

The bigger problem is that this is the wrong test. If you'd fixed the first part and you entered the right username and the right password, they'd both be True and that's great. However, if you use the wrong username and the wrong password they'd both be false, and it logs right in because False == False.

You'd only get stuck if you enter one correct and the other wrong.

Instead of checking whether they're equal, have a quick read about boolean operators such as and and or.

 

There may be other issues but I think that should start you off.


Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.

#3 Gotcha_Hotshot22

Gotcha_Hotshot22
  • Topic Starter

  • Members
  • 77 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:27 PM

Posted 09 August 2014 - 05:13 PM

Thank you for pointing that out! I feel kinda dumb now...lol

 

So here's what I came up with in the light...

import sys
def AgeCalc():
#EXAMPLE FUNCTION THAT IS CALLED AFTER LOGGING IN

        dateofbirth=input("Year you were born: ")
        dateofbirth= int(dateofbirth)
        currentyear=input("Current year: ")
        currentyear= int(currentyear)
        print("You are either",currentyear-dateofbirth-1, "or",currentyear-dateofbirth)

#END EXAMPLE
#LOGGED IN FUNCTION BELOW

def loggedin():
        print("Welcome!")
        print(" ")
        print("Your choices are...")
        print(" ")
        print("(A) Age Calculator")
        print("(B) Log Out")
        print(" ")
        choice=input("Menu Choice: ")
        if (choice== "a" or "A"):
                AgeCalc()
        while choice== ("a" or "A"):
                loggedin()

#LOGGED IN FUNCTION ABOVE
#LOGIN FUNCTION BELOW
        
def login():
        Errorlevel= int(3)

        while (Errorlevel >=1):
                print("Please log in...")
                print(" ")
                print("You have",Errorlevel,"attempts remaining.")
                un= input("Username: ")
                print(" ")
                pw= input("Password: ")
                if (un=="Daniel" and pw=="ABC"):
                        loggedin()
                        
                Errorlevel= int(Errorlevel-1)
        
        while (Errorlevel <=0):
                sys.exit(0)
#LOGIN FUNCTION ABOVE
#BELOW CALLS THE LOGIN FUNCTION

login()


Thanks,

Daniel





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users