Python Forum
what's wrong? i know this is simple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what's wrong? i know this is simple
#1
Ok, I'm a struggling programmer. Dabbled in it for years and never get any better than a basic hello world programmer. LOL.

I'm trying to write a script that tests me on a password. I'm terrible at remembering passwords and I shouldn't be writing down my admin passwords. They should be in my head. So, I was searching for a program that would test me and found there are none. So, I figured, maybe I'll see if I can figure it out in Python. The script worked until I put the clear statement in it. I'm trying to clear the screen so I can't look at it when it runs again... I need to memorize it. But, for whatever reason I'm not seeing, when I put the clear there, it asks the question real quick "what's my password" then clears the screen. I'd think since it's below the input line, it would display the question and wait for me to input something before it moves on to the next line to clear it. But it's not even making it to the run again part. The clear screen is wiping out the prompt to ask for the password.

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import os

repeat = "y"

while repeat=="y":

    mypassword = "test123"
    guesspassword = input("What is the password: ")
    if guesspassword==mypassword:
        print("match")
        input("Press Enter")

    else:
        print("no match")
        print("Password is ", mypassword)
        input("Press Enter")

    repeat = input("Run again?")
    os.system('cls')
If you all told me I just found a bug in python, that would make my day. I know I'm doing something elementary stupidly wrong.
Reply
#2
Don't know if it's your issue but you are reassigning repeat
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Mar-15-2023, 01:07 AM)menator01 Wrote: Don't know if it's your issue but you are reassigning repeat

well, I'm asking if want to run it again and assigning it to repeat and if repeat is y, then run it again. Is there something I'm doing wrong here. Looks perfectly correct to me.... but in the same token, I have a very stupid error I'd be able to see if I was good at this. So, obviously me thinking it's right means nothing... lol.
Reply
#4
Tried moving the assignment out of the while statement. made no sense with it there. should be at top.

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import os

repeat = "y"
input("set mypassword")
mypassword = "test123"

while repeat=="y":
    input("stsart while")

    guesspassword = input("What is the password: ")
    if guesspassword==mypassword:
        input("start if")
        print("match")
        input("Press Enter")

    else:
        input("start else")
        print("no match")
        print("Password is ", mypassword)
        input("Press Enter")

    repeat = input("Run again?")
    os.system('cls')
Reply
#5
code works fine with one run. then on the second run, it dies on this..

input("stsart while")

guesspassword = input("What is the password: ")

it blinks "What's my password" and then clears the screen.
Reply
#6
Ok, just got it to work. I had to add a sleep statement. It's like the python script is running so fast it just blasts past the input and clears it. First tried to delete the guess variable to see if that was it, but it didn't make a difference. Doesn't make sense to me. But below works...

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import os,time

repeat = "y"
mypassword = "test123"

while repeat=="y":
    guesspassword = input("What is the password: ")
    if guesspassword==mypassword:
        print("match")
        input("Press Enter")

    else:
        print("no match")
        print("Password is ", mypassword)

    repeat = input("Run again?")
    del guesspassword
    os.system('cls')
    time.sleep(2.0)
Reply
#7
BTW, I now have a working program to help you all remember your passwords. Who wants to buy it?
Reply
#8
Here is another way of doing it

import os

# Set a password variable
mypassword = 'test123'

# Define a function to clear the terminal
def clear():
    return os.system('cls' if os.name == 'nt' else 'clear')

# Start the loop
while True:
    # Clear the terminal and ask for password
    clear()
    password = input('What is the password?: ')

    # Check if password matches
    if password == mypassword:
        # Password matches do stuff
        print('Password matched')
    else:
        # Password does not match
        print('Password does not match')
        print(f'Password is {mypassword}')

    # Ask to go again ask is forced to lower case
    ask = input('Try again?: ').lower()

    # if ask is y or yes go again else exit
    if ask in ['yes', 'y']:
        continue
    else:
        clear()
        print('Goodbye!')
        break
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
Another way. generates a random 10 character password

import os
from random import choices
from string import digits, ascii_letters

# Define a function to generate and return a 10 character password
def generate():
    characters = digits+ascii_letters+'@#$&!'
    return ''.join(choices(characters, k=10))

# Define a function to clear the terminal
def clear():
    return os.system('cls' if os.name == 'nt' else 'clear')

# Start the loop
while True:
    # Clear the terminal, generate a password and ask for password
    clear()
    mypassword = generate()
    password = input('What is the password?: ')

    # Check if password matches
    if password == mypassword:
        # Password matches do stuff
        print('Password matched')
    else:
        # Password does not match
        print('Password does not match')
        print(f'Password is {mypassword}')

    # Ask to go again ask is forced to lower case
    ask = input('Try again?: ').lower()

    # if ask is y or yes go again else exit
    if ask in ['yes', 'y']:
        continue
    else:
        clear()
        print('Goodbye!')
        break
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#10
That's cool. I was thinking me statically putting in my super sensitive password in my script in plain text is a huge security risk. Problem with your solution though is that it would make sense to me that everytime the script runs, it will generate a different one. I need to learn it and it will take me longer than one script run to remember it.

I was trying to figure out how maybe I could ask for the admin password and then store it in the script or a file encrypted. But then, how is it encrypted? Do I need to remember a master password so I can decrypt it? How do I remember that? Lol. Catch 22. Need to keep it in there unencrypted. I guess the idea is it's on my phone only. I'm running right now on a python ide on my phone with the code local. So, short of someone stealing my phone and breaking into it or hacking my phone over wifi or cellular to grab the locally stored files, they cant get it. And, if they're looking to hack, seems like their time would be better spent hacking somewhere in the cloud where there are more gains to be made rather than my phone files.


(Mar-15-2023, 08:50 AM)menator01 Wrote: Another way. generates a random 10 character password

import os
from random import choices
from string import digits, ascii_letters

# Define a function to generate and return a 10 character password
def generate():
    characters = digits+ascii_letters+'@#$&!'
    return ''.join(choices(characters, k=10))

# Define a function to clear the terminal
def clear():
    return os.system('cls' if os.name == 'nt' else 'clear')

# Start the loop
while True:
    # Clear the terminal, generate a password and ask for password
    clear()
    mypassword = generate()
    password = input('What is the password?: ')

    # Check if password matches
    if password == mypassword:
        # Password matches do stuff
        print('Password matched')
    else:
        # Password does not match
        print('Password does not match')
        print(f'Password is {mypassword}')

    # Ask to go again ask is forced to lower case
    ask = input('Try again?: ').lower()

    # if ask is y or yes go again else exit
    if ask in ['yes', 'y']:
        continue
    else:
        clear()
        print('Goodbye!')
        break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 439 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,383 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 2,940 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  simple dragon realm gone wrong Darbandiman123 7 5,491 Mar-01-2017, 05:25 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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