I want the user to be able to input their username beside the "username: " at the center but the results always be:
username: example
The "username: " is center which is what I want but the input text "example" is always not beside the "username:"
My code is below:
username = input("Username: ".center(165))
My desired outcome is:
username: example
with it being in the center of the page.
By doing input("Username: ".center(165)) you create a string of length 165 where "Username: " is placed right at the middle. So you get many spaces to the right of that string.
You want the "Username: " string to be at the right-end of the full string, so you should use rjust instead:
username = input("Username: ".rjust(165//2))
Related
I have something like an e-mail storage and I want that the saved information printed out in my Label, but I want that the information stacked among each other.
This is my code, but when I enter another e-mail and password, the current Label text is replaced.
How can I fix that?
def print_data(mail,passwort):
label_list["text"] = str(mail)+" | "+str(passwort)
def save_info():
mail = entry_mail.get()
passwort = entry_passwort.get()
entry_mail.delete(0, tk.END)
entry_passwort.delete(0, tk.END)
print_data(mail,passwort)
you have to store the previous information in a variable. Then
use:
label.configure(text= "previous label" + "new text")
The dictionary
{'password': ['Input data fields to enter - 1) Username 2) Password 3) Re-enter Password 4) Security question 5) Security answer\nUsername data field specifications - At least one uppercase, \nUsername data field specifications - At least one lowercase.',
'Password data field specifications - At least 7 characters, \nPassword data field specifications - 1 number, \nPassword data field specifications - 1 uppercase letter, \nPassword data field specifications - 1 lowercase letter, \nPassword data field specifications - one special charactersss.',
'Password criteria should be displayed to the user when user clicks inside the password data field.',
"Green check mark next to 'Password' data field should be displayed that indicates to the user after typing password that the entry meets criteria.",
"Red cross mark next to 'Password' data field should be displayed that indicates to the user after typing password that the entry does not meet criteria.",
"Validate that inputs in 'Password' and 'Re-enter Password' data fields match- Green check mark next to 'Re-enter' password data field should be displayed that indicates to the user after typing if passwords match\n'Password' and 'Re-enter Password' entries should be masked.",
'Password entries will show the keystroke typed, and then after two seconds the entered character is masked.',
'If \'Password\' entry does not match criteria specified and user hits Submit, show error alert "Password entry does not meet criteria".',
"If entries in 'Password' and 'Re-enter Password' do not match and user hits Submit, show error alert 'Password entries do not match'."]}
Needed to a data-frame by converting these list of values into a DF with same key
I am not really sure about what you want but
import pandas as pd
dataframe = pd.DataFrame('Your dictionary comes here')
Additionally:
If you have just an array and you need to put it into a dataframe
import pandas as pd
dataframe = pd.DataFrame() # Without any arguments
dataframe['The Coulmn name'] = 'Your array comes here'
I am not sure about your desired out put, may be you wanted something like this.
import pandas as pd
df = pd.DataFrame({'password': ['Input data fields to enter - 1) Username 2) Password 3) Re-enter Password 4) Security question 5) Security answer\nUsername data field specifications - At least one uppercase, \nUsername data field specifications - At least one lowercase.',
'Password data field specifications - At least 7 characters, \nPassword data field specifications - 1 number, \nPassword data field specifications - 1 uppercase letter, \nPassword data field specifications - 1 lowercase letter, \nPassword data field specifications - one special charactersss.',
'Password criteria should be displayed to the user when user clicks inside the password data field.',
"Green check mark next to 'Password' data field should be displayed that indicates to the user after typing password that the entry meets criteria.",
"Red cross mark next to 'Password' data field should be displayed that indicates to the user after typing password that the entry does not meet criteria.",
"Validate that inputs in 'Password' and 'Re-enter Password' data fields match- Green check mark next to 'Re-enter' password data field should be displayed that indicates to the user after typing if passwords match\n'Password' and 'Re-enter Password' entries should be masked.",
'Password entries will show the keystroke typed, and then after two seconds the entered character is masked.',
'If \'Password\' entry does not match criteria specified and user hits Submit, show error alert "Password entry does not meet criteria".',
"If entries in 'Password' and 'Re-enter Password' do not match and user hits Submit, show error alert 'Password entries do not match'."]})
print df
Output:
password
0 Input data fields to enter - 1) Username 2) Pa...
1 Password data field specifications - At least ...
2 Password criteria should be displayed to the u...
3 Green check mark next to 'Password' data field...
4 Red cross mark next to 'Password' data field s...
5 Validate that inputs in 'Password' and 'Re-ent...
6 Password entries will show the keystroke typed...
7 If 'Password' entry does not match criteria sp...
8 If entries in 'Password' and 'Re-enter Passwor
I'm printing some variables, like this:
print("First name:", first_name)
print("Last name:", last_name)
print("Password:", password)
The first two are displayed just fine but the last one is like this:
Password:
<the password>
Which is undesirable and inconsistent with the other ones, which is why I want it to look like this:
Password:<the password>
Using end="" did not help.
Try to convert the password into string inside the print func
print("password:",str(password))
Your password variable contains a linebreak (or its string representation does).
Either of these formatting methods will print a string variable inline with no linebreak:
>>> password = "blarf"
>>> print("password:", password)
password: blarf
>>> print(f"password: {password}")
password: blarf
If the variable contains a linebreak, though, you'll get a linebreak in the output:
>>> password = "\nblarf"
>>> print("password:", password)
password:
blarf
Try:
print("password:",password.strip())
Your password variable content likely has a linebreak before it. strip gets rid of that. Note that it also removes all whitespaces before and after the string characters in password, including before and after spaces. If you wish to only remove linebreaks from before and after, use:
print("password:",password.strip("\n"))
Try and remove any newline char that exist in the password string.
You can do so using the following:
first_name = "david"
last_name = "s"
password = "\n1234"
print("firstname:",first_name)
print("lastname:",last_name)
print("password:",password)
# firstname: david
# lastname: s
# password:
# 1234
# removing the new line
password = password.replace("\n","")
print("firstname:",first_name)
print("lastname:",last_name)
print("password:",password)
# firstname: david
# lastname: s
# password: 1234
import re
re.sub("\n", "", password)
print("firstname:",first_name)
print("lastname:",last_name)
print("password:",password)
# firstname: david
# lastname: s
# password: 1234
The first option is using replace method of string object which you can read about in the following link:
https://www.tutorialspoint.com/python/string_replace.htm
The second option is using sub of the re module which you can read about in the following link:
https://note.nkmk.me/en/python-str-replace-translate-re-sub/
Managed to resolve the problem thanks to Samwise I used :
password.splitlines()
password = password[1]
print("password:",password)
David S method works fine too
password = password.replace("\n","")
print("password:",password)
I'm trying to get through my first semester of Python and am struggling. My dictionary is correct however, I cannot get what I want. I need the user to input a patient ID and then the program will display the ID with the name linked to it in the dictionary as the name is the value. Then if the user enters an ID that is not in the dictionary the computer tells them so. If they enter done the program says DONE! When numbers are entered the program needs to continue asking the question until done is typed. Here is was I have so far:
patientinfo = {}
lines = open('PatientsNames.txt')
lines.readline()
for line in lines:
id=line[:8]
if id not in patientinfo:
endlastname = line.find(' ', 8)
lastname = line[8:endlastname]
firstname = line[endlastname+1:]
patientinfo[id] = lastname + ', ' + firstname
answer = raw_input("Enter an ID ('done' to exit)")
try:
if answer in patientinfo.keys():
print answer, patientinfo(val)
except:
if answer not in patientinfo.keys():
print 'No patient with that ID'
else:
if answer='done':
print 'DONE!'
I have this Python code:
with open('save.data') as fp:
save_data = dict([line.split(' = ') for line in fp.read().splitlines()])
with open('brute.txt') as fp:
brute = fp.read().splitlines()
for username, password in save_data.items():
if username in brute:
break
else:
print("didn't find the username")
Here is a quick explanation; the save.data is a file that contains variables of Batch-file game (such as username, hp etc...) and brute.txt is a file that contains "random" strings (like what seen in wordlists used for brute-force).
save.data:
username1 = PlayerName
password1 = PlayerPass
hp = 100
As i said before, it's a Batch-file game so, no need to quote strings
brute.txt:
username
usrnm
username1
password
password1
health
hp
So, let's assume that the Python file is a "game hacker" that "brute" a Batch-file's game save file in hope of finding matches and when it does find, it retrieves them and display them to the user.
## We did all the previous code
...
>>> print(save_data["username1"])
PlayerName
Success! we retrieved the variables! But I want to make the program capable of displaying the variables it self (because I knew that "username1" was the match, that's why I chose to print it). What I mean is, I want to make the program print the variables that matched. E.g: If instead of "username1" in save.data there was "usrnm", it will surely get recognized after the "bruting" process because it's already in brute.txt. So, how to make the program print what matched? because I don't know if it's "username" or "username1" etc... The program does :p (of course without opening save.data) And of course that doesn't mean the program will search only for the username, it's a game and there should be other variables like gold/coins, hp etc... If you didn't understand something, kindly comment it and I will clear it up, and thanks for your time!
Use a dict such as this:
with open('brute.txt', 'r') as f:
# First get all the brute file stuff
lookup_dic = {word.strip(): None for word in f.readlines()}
with open('save.data', 'r') as f:
# Update that dict with the stuff from the save.data
lines = (line.strip().split(' = ') for line in f.readlines())
for lookup, val in lines:
if lookup in lookup_dic:
print(f"{lookup} matched and its value is {val}")
lookup_dic[lookup] = val
# Now you have a complete lookup table.
print(lookup_dic)
print(lookup_dic['hp'])
Output:
username1 matched and its value is PlayerName
password1 matched and its value is PlayerPass
hp matched and its value is 100
{'username': None, 'usrnm': None, 'username1': 'PlayerName', 'password': None, 'password1': 'PlayerPass','health': None, 'hp': '100'}
100