I want to write a Python program that will be able to read from a dictionary into any (or most) textboxes. Will this be possible and how would I approach it?
Here is an example of what I mean with the first line being an example of a command I'd like to be able type to receive the 2nd line which would be the message stored in the dictionary value:
/alias hello
HELLO! THIS IS AN AUTOMATED REPLY! :D
Store the dictionary as key,value pair in the form {key:value}. In the key part write your commands like 'hello' and in the value part give your corresponding message like ' HELLO! THIS IS AN AUTOMATED REPLY! :D'. You can access your value from its key. For eg:
abc = {"hello" : "HELLO! THIS IS AN AUTOMATED REPLY! :D"}
print abc["hello"]
It will print "HELLO! THIS IS AN AUTOMATED REPLY! :D".
I think this is what you meant. Actually I haven't understood what you meant by textboxes..
Related
I have a question regarding my python programming. Before I ask a question, here is the instructions that I have to complete:
[Write a program to read through the example_messages.txt file and figure out who has sent the greatest number of mail messages.
The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file.]
I attached a PDF file URL of the kind of program that I created. I also tried to attach the "example_messages" text file to Stack Overflow but wouldn't let me. So instead, I attached a photo instead.
[Program that I created] https://ibb.co/nm3dBYt
[Photo of example_messages.txt] https://ibb.co/qkmfrLn
I used the “Counter(email_lst).most_common()” function in my program to complete the task. This method works, but based on the assignment, I have to use the dictionary to complete the task, and I am having a difficult time coming up with any ideas when using dictionaries. The program should be no more than 10 lines of code. Does anyone have any ideas or suggestions?
Best Regards
You can "manually" add 1 to entries in the dictionary and use the max() function at the end to get the entry with the highest count:
counts = dict()
with open('example_messages.txt') as f:
for line in f.readlines():
if not line.stastswith("From"): continue
email = line.split()[1]
counts[email] = counts.get(email,0) + 1
email,count = max(counts.items(),key=lambda ec:ec[1])
print(email,"hase themost sent emails which is",count)
I've been working on a login and signup code that is not connected to the cloud. To login, you must enter a username and password, and it will return you with a simple; "Hello, "
I've tried converting the element to a string, but I couldn't find anything else online to help me. It could possibly be that the program I'm using doesn't display an output properly.
inorup=""
newuser=""
users=["jdoe,tractor5","carrey1,1997","admin,56f67dk2997m"]
firstnuser=["John","Carrey","Frederick"]
while inorup != "login" or inorup != "signup":
inorup=raw_input("Would you like to login or signup? ")
if inorup=="login":
userpass=raw_input("Enter <username,password>: ")
if userpass in users:
userpassfnamepos=int(users.index(userpass))-3
print("Hello",str(firstnuser[userpassfnamepos])+".")
#Returning:
Would you like to login or signup? login
Enter <username,password>: jdoe,tractor5
('Hello', 'John')
#Expected Return:
Would you like to login or signup? login
Enter <username,password>: jdoe,tractor5
Hello John.
You should split your string on the ',' and check username password against a dictionary like {username:password}, or use a database or something. Checking string as you do now is kind of limiting and unwieldy.
But to just fix your output, you can change your print to this
print("Hello {}.".format(firstnuser[userpassfnamepos]))
using string.format() also does all the casting and such so you could pass in numbers or whatever you want, not just name. More info Here
and you will get the expected output.
output =
Would you like to login or signup? login
Enter :
jdoe,tractor5
Hello John.
You are already using normal string concatenation to add the period. Why not just do that with the name as well? You also don't need the str in front, since the list is already a list of strings, and so the element in question is already a string.
print("Hello" + " " + firstnuser[userpassfnamepos] + ".")
>>>Would you like to login or signup? login
>>>Enter <username,password>: jdoe,tractor5
>>>Hello John.
Looks like you are mixing python 2 and 3. raw_input id python 2 and in Python 3 it's only input. also, you should use brake to break out of the loop at appropriate places. You have an infinite loop. Lately, the code is printing "Hello John" for me as it's supposed to. However, you don't have to convert the list element to string, it is already a string (therefore returns the same value with or without converting.
I think the reason it's giving you ('Hello', 'John') as output is because it's printing the parenthesis themselves (but that does not make logical sense because they are not included in the string. Anyways note that python 2 print statement is as follows: print "this is to be printed" . there is no parenthesis. Make use of the vertion of python you are using and that your code matches the version.
One additional note, if you are not using a database, maybe use a python dictionary instead of a python list. Not only it's faster and memory efficient, it also makes it easier to work with. it can be something like {username : ("name" , "password")} a combination of a dictionart and a tuple or more explicit {username : {name: "name" , password: "password"}}, which saves a sigle's user data in a dictionary inside a dectionary containing all the users.
i am trying to create a program which is a computer diagnostic service. I want to be able to ask the user what their problem is then extract key words from it. Then I want to print a solution. For example, the user says "My screen is broken", the program recognises "screen" and prints the solution for a broken screen. I honestly have no idea how to do this and i really need some help. Thanks!
with some dictionary of keywords to solutions d,
d = {'screen': 'Get a new screen', ...}
problem = input('What did you do? ').lower()
for k in d:
if k in problem:
print(d[k])
For each keyword, check if it's in the problem. If it is, print the associated solution
This works too
import re
D = {'screen': 1, 'keyboard': 2, 'mouse': 3}
keywords = set(D)
wordre = re.compile(r'\w+')
problem = "The cursor doesn't move on the screen when I move the mouse"
found = set(wordre.findall(problem.lower())) & keywords
print(found) # prints {'mouse', 'screen'}
Your question doesn't specify if your code will impose any limitations with regards to the extent of the user's input.
Assuming that the user will be able to describe his problem to a great extent (i.e input raw text as opposed to just typing a sentence or two) you can use the summa module.
If you take a look at its documentation, you will see that by applying its keywords function on any text; you can extract keywords out of it. Consequently, you can parse those arguments to print the respective solutions. A simple way to do this is to maintain a dictionary with your keywords of interest as keys and their solutions as values; and then just simply cross-check the summa generated keywords against these to print your final solution.
As part of my project, I want to ask the user to enter an order from existing orders in a list.
So this is the problem: If my user writes the order in the input not exactly as its written in the list, how will my program understand it?
In other words, I want to turn this into Python code:
if (part of) string in order_list:
How can I do this?
order = input()
orders_list = ["initiate", "eat", "run", "set coords to"]
#Here is the problem
if order in orders_list:
#my output
For example, lets say I entered "eating" instead of "eat". How will my code
understand that I meant "eat"?
You can see if any of your words are contained in the users word like this:
if any(word in order for word in orders_list):
#my output
>>> from difflib import get_close_matches
>>> orders_list = ["initiate", "eat", "run", "set coords to"]
>>> get_close_matches('eating', orders_list)
['eat']
The fact that you are struggling with this should be an indication that your data structure is wrong.
Instead of getting the user to write a single string, get them to enter individual elements and store them separately in a list.
im building a test program. its essentially a database of bugs and bug fixes. it may end up being an entire database for all my time working in python.
i want to create an effect of layers by using a dictionary.
here is the code as of april 29 2011:
modules=['pass']
syntax={'PRINT':''' in eclipse anthing which
you "PRINT" needs to be within a set of paranthesis''','StrRet':'anytime you need to use the return action in a string, you must use the triple quotes.'}
findinp= input('''where would you like to go?
Dir:''')
if findinp=='syntax':
print(syntax)
dir2= input('choose a listing')
if dir2=='print'or'PRINT'or'Print':
print('PRINT' in syntax)
now when i use this i get the ENTIRE dictionary, not just the first layer. how would i do something like this? do i need to just list links in the console? or is there a better way to do so?
thanks,
Pre.Shu.
I'm not quite sure what you want, but to print the content of a single key of dictionary you index it:
syntax['PRINT']
Maybe this help a bit:
modules=['pass']
syntax={
'PRINT':''' in eclipse anthing which
you "PRINT" needs to be within a set of paranthesis''',
'STRRET':'anytime you need to use the return action in a string, you must use the triple quotes.'}
choice = input('''where would you like to go?
Dir:''').upper()
if choice in syntax:
print syntax[choice]
else:
print "no data ..."