I'm looking to create a Python based vocabulary checker for my little cousin to use for studying. The purpose of the program will be to display a word and then she will need to type in the definition and have it checked. I was wondering if the best way to do this is with array lists:
vocab = ['Python','OSX']
definition = ['programming language','operating system']
Is this the best way to go about this? And if so how do I have the program randomly display a vocab and then check the definition. Any help would be greatly appreciated. Thank you guys.
Ok. So this is what I have so far....
#Russian Translation Program
import os
import random
#Asks users if they want to add more vocabulary
word_adder=raw_input("Add more words? If yes, press 1: ")
with open("Russian_study.txt","a") as f:
while word_adder=="1":
word=raw_input("Enter word: ")
translation=raw_input("Word translation: ")
f.write("'{0}':{1},".format(word,translation))
word_adder=raw_input("Add another word? If yes, press 1: ")
#Checks to see if file exists, if not one is created
with open("Russian_study.txt","a") as f:
pass
os.system('clear')
print("Begin Quiz")
#Begin testing user
with open("Russian_study.txt","r") as f:
from random import choice
question = choice(list(f))
result = raw_input('{0} is '.format(question))
print('Correct' if result==f[question] else ':(')
However, my output is
Begin Quiz
'Один':'One', is
How do I make it only display Один and check the user input against one?
use a dictionary:
d={'Python':'programming language', 'OSX':'operating system'}
from random import choice
q = choice(list(d))
res = input('{0} is:'.format(q))
print('yay!' if res == d[q] else ':(')
[if you are using python < 3.0, use raw_input() instead of input()]
the simplest (and not safe!) way to write/read from a file:
with open('questions.txt', 'w') as f:
f.write(repr(d))
'questions.txt' will have this line:
`{'Python':'programming language', 'OSX':'operating system'}`
so for reading it you can do
with open('questions.txt') as f:
q=eval(f.read())
and now q and d are equal. don't use this method for "real" code, as 'questions.txt' may contain malicious code.
1) You can use random.choice() to randomly pick an element from your vocab list (or the keys() of a dictionary).
2) Deciding when an answer is close enough to the definition is trickier. You could simply search the answer string for certain key words. Or if you want to get more complex, you can calculate something like the Levenshtein distance between two strings. You can read about the L distance here: http://en.wikipedia.org/wiki/Levenshtein%5Fdistance. And there are python recipes online for calculating the L distance.
Related
I'm really new in Coding, with Python.
I was trying to make a Vocabulary exercise program for a Language that i am learning it right now. So the concept is, if the word "abhängen" is shown at the Console, i have to write "von" which is the right word to come after that word, which is "abhängen". And the program will show if its right or wrong, and loops the input to get the right answer.
But since there are tons of vocabulary, i have to make same loop over and over again just by using while True and changing a,b,c for the variables and the word between "". Is there a way to make it shorter maybe by using list or something?
And if its possible, can i somehow make the order of the questions randomly? Since this code always shows the first question as abhängen and second as abrechnen.
Sorry if this was some kind of dumb question to ask, have nowhere to ask haha
have a nice day guys
while True:
a = input("abhängen ")
if a == "von":
print("You're right")
break
else:
print("Wrong")
while True:
c = input("abrechnen ")
if c == "mit":
print("You're right")
break
else:
print("Wrong")
It looks like a great example to learn usage of lists in Python.
You can use list of tuples and random module for this task. See random.shuffle documentation here
words_and_answers = [("abhängen ", "von"), ("abrechnen ", "mit")]
random.shuffle(words_and_answers)
for input_word, correct_answer in words_and_answers:
while True:
user_response = input(input_word)
if user_response == correct_answer:
print("You're right")
break
else:
print("Wrong")
I think a possible solution is to use a dictionary in which the keys are the words "abhängen", "abrechnen", ecc... And the values are the correct words that come after. So you can write a thing like this:
vocabulary = {
"abhängen" : "von",
"abrechnen" : "mit"
}
for i in vocabulary:
a = input(i + " ")
if a == vocabulary[i]:
print("You are right")
else:
print("Wrong")
Note that in this case the loop is not infinite! This method allows you to have more than one word correct for each key
For the randomly access to the keys, you can make a list with the keys and use the random.shuffle() method, like this:
import random
keys = list(vocabulary.keys())
random.shuffle(keys)
And then your loop will be:
for i in keys:
...
I want to solve the mime challenge in coding games.com. My code can pass all the test but not the optimisation test.
I tried to remove all useless functions like parsing to string but the problem is on the way I think about it.
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
n = int(input()) # Number of elements which make up the association table.
q = int(input())
# Number Q of file names to be analyzed.
dico = {}
# My function
def check(word):
for item in dico:
if(word[-len(item)-1:].upper() == "."+item.upper()):
return(dico[item])
return("UNKNOWN")
for i in range(n):
# ext: file extension
# mt: MIME type.
ext, mt = input().split()
dico[ext] = mt
for i in range(q):
fname = input()
fname = fname
print(check(fname))
# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)
#print("Debug message...", file=sys.stderr)
Failure
Process has timed out. This may mean that your solution is not optimized enough to handle some cases.
This is the right idea, but one detail appears to be destroying the performance. The problem is the line for item in dico:, which unnecessarily loops over every entry in the dictionary. This is a linear search O(n), checking for the target item-by-item. But this pretty much defeats the purpose of the dictionary data structure, which is to offer constant-time O(1) lookups. "Constant time" means that no matter how big the dictionary gets, the time it takes to find an item is always the same (thanks to hashing).
To draw a metaphor, imagine you're looking for a spoon in your kitchen. If you know where all the utensils, appliances and cookware are are ahead of time, you don't need to look in every drawer to find the utensils. Instead, you just go straight to the utensils drawer containing the spoon you want, and it's one-shot!
On the other hand, if you're in someone else's kitchen, it can be difficult to find a spoon. You have to start at one end of the cupboard and check every drawer until you find the utensils. In the worst-case, you might get unlucky and have to check every drawer before you find the utensil drawer.
Back to the code, the above snippet is using the latter approach, but we're dealing with trying to find something in 10k unfamiliar kitchens each with 10k drawers. Pretty slow, right?
If you can adjust the solution to check the dictionary in constant time, without a loop, then you can handle n = 10000 and q = 10000 without having to make q * n iterations (you can do it in q iterations instead--so much faster!).
Thank you for your help,
I figured out the solution.
n = int(input()) # Number of elements which make up the association table.
q = int(input()) # Number Q of file names to be analyzed.
dico = {}
# My function
def check(word):
if("." in word):
n = len(word)-(word.rfind(".")+1)
extension = word[-n:].lower()
if(extension in dico):
return(dico[extension])
return("UNKNOWN")
for i in range(n):
# ext: file extension
# mt: MIME type.
ext, mt = input().split()
dico[ext.lower()] = mt
for i in range(q):
fname = input()
print(check(fname))
Your explanation was clear :D
Thank you
I have a class for data entry that requires a lot of input from the user. I use it to semi-automate the process of putting stuff in the db where it is possible.
My instinct is to put it in my model classes, and write tests on it, but it would be an insane amount of work, and I have a lot of raw_input() functions and logic loops that I don't know how to test or what to do with.
Should I keep this module separate or try to include it in the model classes?
def define(self, word=False, word_pk=False):
'''Defining a word, there may be language specific elements to edit in here'''
try:
if word_pk:
word = Word.objects.get(id=word_pk)
else:
word = Word.objects.get(language__name=self.language_ISO, name=word)
except:
return "Word lookup failed for word=%s word_pk=%s\n" % (word, word_pk)
print "\n\tThe Word is: '%s'...\n" % (word)
wiktionary_list = word.wiktionary_lookup(self.wiktionary_prefix, self.driver)
wn_tuple = word.wn_lookup()
while choice("Would you like to add a/another definition for '%s'?: " % word):
#Ask the user if they want to use the wn output for definitions, make them select which ones
if choice("Would you like to choose a wordnet definition?: "):
chosen_defs = ask_input("Which ones? (choose all that apply with a space between numbers): ")
chosen_defs = [int(i) for i in (chosen_defs.split())]
#Wornet only gives part of speech and definition information so I need to split that here.
for i in chosen_defs:
#Print_n_save function will return False if it exits successfully, so there is an option to repeat this loop if the user makes a mistake somewhere
repeat = True
while repeat:
tup = wn_tuple[i]
print "\n(%s) - %s\n" % (tup[0], tup[1])
audio_tup = self.add_audio(word)
picture_tup = self.add_picture(word)
new_definition = Definition()
new_definition.word=word
new_definition.part_speech= tup[0]
new_definition.definition=tup[1]
new_definition.def_source="Wordnet"
new_definition.add_pronunciation()
new_definition.word_audio=audio_tup[0]
new_definition.audio_source=audio_tup[1]
new_definition.picture=picture_tup[0]
new_definition.pic_source=picture_tup[1]
repeat = self.print_n_save(new_definition)
elif choice("Would you like to choose a wiktionary definition?: "):
choose_defs = ask_input("Which ones would you like to choose? (Numbers separated by spaces): ")
chosen_defs = [int(i) for i in choose_defs.split()]
for i in chosen_defs:
#Print_n_save function will return False if it exits successfully, so there is an option to repeat this loop if the user makes a mistake somewhere
repeat = True
while repeat:
print "\n%s\n" % (wiktionary_list[i])
audio_tup = self.add_audio(word)
picture_tup = self.add_picture(word)
new_definition = Definition()
new_definition.word=word
new_definition.get_pos()
new_definition.definition=wiktionary_list[i]
new_definition.def_source="Wiktionary"
new_definition.add_pronunciation()
new_definition.word_audio=audio_tup[0]
new_definition.audio_source=audio_tup[1]
new_definition.picture=picture_tup[0]
new_definition.pic_source=picture_tup[1]
repeat = self.print_n_save(new_definition)
else:
#Print_n_save function will return False if it exits successfully, so there is an option to repeat this loop if the user makes a mistake somewhere
repeat = True
while repeat:
#Asking for definition, inputting raw from some internet source
definition = ask_input("What is the definition?: ")
definition_source = ask_input("What is the source of the definition?: ")
audio_tup = self.add_audio(word)
picture_tup = self.add_picture(word)
new_definition = Definition()
new_definition.word=word
new_definition.get_pos()
new_definition.definition=definition
new_definition.def_source=definition_source
new_definition.add_pronunciation()
new_definition.word_audio=audio_tup[0]
new_definition.audio_source=audio_tup[1]
new_definition.picture=picture_tup[0]
new_definition.pic_source=picture_tup[1]
repeat = self.print_n_save(new_definition)
Don't try to force a raw python function into a single box.
What you should have done (a long long time ago), is separate it out into separate functions so it would be easier to test and figure things out.
Since you're asking for user input, websites do that through forms, so you're going to need a form - or a form wizard/set/whatever.
That form is going to need at least one view to handle it, so you might need to write that too, or use a generic view.
Who knows, the model might even need to do something post processing (I didn't really read the code)
I would put this into management/commands. Just wrap your functions into a BaseCommand class and you are good to go. And here is how to make testing.
I want to create a list from a user's input and then return a random value from that list.
This is what I have so far for the code but I can't figure out how to get input for the array.
import random
movie_array = ["movieA", "movieB", "movieC"]
print(random.choice(movie_array))
I know about the input function but I'm not sure how to use it to create an array/list. I tried the following but it didn't work
movie_array = input()
but when I run the random.choice on that it only selects an individual character.
You can do it like this:
>>> import random
>>> movie_array = [input("Input a movie: ") for i in range(3)]
Input a movie: movieA
Input a movie: movieB
Input a movie: movieC
>>> print(random.choice(movie_array))
movieC
Use a loop.
In the body of the loop prompt the user to enter a movie title, or 'q' to quit. Append each entry to your movie list. When finished select a random movie from the movie list:
import random
movies = []
while True:
entry = input('Enter a movie title (q to quit): ')
if entry.lower() == 'q':
break
movies.append(entry)
if movies:
print(random.choice(movies))
Hopefully your users do not want to enter the movie entitled "Q" (1982). It might be better to use an empty entry as the sentinel, like this:
entry = input('Enter a movie title (<Enter> to quit): ')
if entry == '':
break
You can read movies in a single line separated by commas and then split it into array.
movie_array=raw_input("Enter Movies").split(',')
If it is python 3.x
movie_array=input("Enter Movies").split(',')
Just want to clarify why your solution attempt didnt work IE this:
movie_array = input()
It's because the input returns a string I.E a list of substrings so
you can seemovie_array = 'test' asmovie_array = ['t','e','s','t']
print(movie_array[1])
>>>'e'
in order to solve your problem I would recommend #mhawke answer (using a while loop)
By using a while loop you can retrieve multiple inputs and store each of them in a list. Be aware though that a while loop has to be able to "END" or it can cause problems. Therefore you have to have a break somewhere in the while loop.
Otherwise the program would be run indefinetly until you either forced it to shutdown or until it crashes.
Further information about this can be read here:
Tutorialspoint (Strings)
Tutorialspoint (Whileloop)
also checkout the official wiki for strings and control flow.
You can give it like this.
movie_array = [b for b in input().split()]
And then you can get input from user like this,
movieA movieB movieC
I am making a program for my own purposes (a naming program) that completely generates a random name. The problem is I cannot assign a number to a letter, so as a being 1 and z being 26, or a being 0 and z being 25. It gives me a SyntaxError. I need to assign this because the random integer (1,26) triggers a letter (if the random integer is 1, select A) and prints the name.
EDIT:
I have implemented your advice, and it works, I am grateful for this, but I wish to have my program create readable names, or more procedural. Here is an example of a name after I tweaked my program: ddjau. Now that doesn't look like a name, so I want it my program to work as if it were creating REAL names, like Samuel or other common names. Thanks!
EDIT (2):
Thanks, Adam, but I need a sort of 'seed' for the user to enter for the start of the name is. (Seed = A, Name = Adam. Seed = G, Name = George.) Should I do this by searching the file line by line, at the very beginning? If so, how do I do this?
Short Answer
Look into Python dictionaries to allow the 1 = 'a' type assignments. Below I have working example that would generate a random name based on gender and a 'litter'.
Disclaimer
I do not fully understand (via the code) what you're trying to accomplish with char/ord and a random letter. Also note having absolutely no idea of your design goals or requirements, I have made the example more complex than it may need to be for instructional purposes.
Additional Resources
* Python Docs for dictionary
* Using Python dictionary relationship to search both ways
In response to the last edit
If you are looking to build random 'real' names, I think your best bet will be to use a large list of names and just pick a random one. If I were you I'd look into something linking to the census results: males and females. Note that male_names.txt and female_names.txt are a copy of the list found at the census website. As a disclaimer, I'm sure there is a more efficient way to load / read the file. Just use this example as a proof on concept.
Update
Here's a quick and dirty way to seed the random values. Again I am not sure that this is the most pythonic way or most efficient way, but it works.
Example
import random
import time
def get_random_name(gender, seed):
if(gender == 'male'):
file = 'male_names.txt'
elif(gender == 'female'):
file = 'female_names.txt'
fid = open(file,'r')
names = []
total_names = 0
for line in fid:
if(line.lower().startswith(seed)):
names.append(line)
total_names = total_names + 1
random_index = random.randint(0,total_names)
return names[random_index]
if (__name__ == "__main__"):
print 'Welcome to Name Database 2.2\n'
print '1. Boy'
print '2. Girl'
bog = raw_input('\nGender: ')
print 'What should the name start with?'
print 'A, Ab, Abc, B, Ba, Br, etc...'
print ''
l = raw_input('Leter(s): ').lower()
new_name = ''
if bog == '1': # Boy
print get_random_name('male',l)
elif bog == '2':
print get_random_name('female',l)
Output
Welcome to Name Database 2.2
1. Boy
2. Girl
Gender: 2
What should the name start with?
A, Ab, Abc, B, Ba, Br, etc...
Leter(s): br
BRITTA
chr (see here) and ord (see here) are the two functions you're looking for (though you already seem to know about the latter). Follow those links for a more detailed explanation.
The first gives you a one-character string based on the integer, the second does the reverse operaion (technically, it handles Unicode as well, which chr doesn't, though you have unichr for that if you need it).
You can base your code on the following:
ch = "E"
print ord (ch) - ord ("A") + 1 # should give 5 for the fifth letter
val = 7
print chr (val + ord ("A") - 1) # should give G, the seventh letter
I'm not entirely sure what you're trying to do, but you can convert a number into a letter with the chr() function. chr() takes an ASCII code, so if you want to use the range [0, 25] instead you can adapt it like so:
chr(25 + ord('a')) # 'z'