patternpythonMinor
Program that teaches kids Chinese characters
Viewed 0 times
kidsprogramchineseteachesthatcharacters
Problem
I wrote code that chooses a random set of Chinese characters, asks the user what the characters are, and if they meet a certain criteria they level up (ex. get more than 50% correct). I want to know if there is a better way to have fewer lists, better organization, etc.
```
# -- coding: utf-8 --
import random
allnumbers = {"一": "one", "二": "two", "三": "three", "四": "four", "五": "five",
"六": "six", "七": "seven", "八": "eight", "九": "nine", "十": "ten"}
Numbers = {"四": "four", "五": "five", "六": "six", "七": "seven", "八": "eight",
"九": "nine", "十": "ten"}
People = {"我": "I or me", "你": "you", "他": "He or him", "她": "Her or she"}
Family = {"爸": "Dad", "妈": "Mom", "哥": "Older brother", "弟": "Younger brother",
"姐": "Older sister", "妹": "Younger sister"}
NotLearned = [Numbers, People, Family]
Learned = []
Practiced = []
LevelOne = []
LevelTwo = []
Mastered = []
setlist = {"Numbers": Numbers, "People": People, "Family": Family}
allLev = [NotLearned,Learned,LevelOne,LevelTwo,Mastered]
all = [Numbers, People, Family]
charlist = []
set = random.choice(all)
k = set.keys()
k = random.sample(k, len(k))
outof = 0
total = len(set)
while True:
print "Welcome! Just type in the character requested! If you want to exit, type quit."
for x in range(0, len(set)):
char = k[x]
question = raw_input("Write Chinese for \"{}\" (Hint: it's {}): ".format(set[char], char))
if question == char:
print "You got it right!"
outof += 1
else:
print "Sorry, that's incorrect"
score = float(outof)/float(total)
print "You got {}%. Good Job!".format(100 * score)
if set in NotLearned:
print "Good job learning that set!"
NotLearned.remove(set)
Learned.append(set)
if set in Learned:
if score >= .5:
print "Good job practicing!"
Learned.remove(set)
Practiced.append(set)
if set in Practiced:
if score >= .6
```
# -- coding: utf-8 --
import random
allnumbers = {"一": "one", "二": "two", "三": "three", "四": "four", "五": "five",
"六": "six", "七": "seven", "八": "eight", "九": "nine", "十": "ten"}
Numbers = {"四": "four", "五": "five", "六": "six", "七": "seven", "八": "eight",
"九": "nine", "十": "ten"}
People = {"我": "I or me", "你": "you", "他": "He or him", "她": "Her or she"}
Family = {"爸": "Dad", "妈": "Mom", "哥": "Older brother", "弟": "Younger brother",
"姐": "Older sister", "妹": "Younger sister"}
NotLearned = [Numbers, People, Family]
Learned = []
Practiced = []
LevelOne = []
LevelTwo = []
Mastered = []
setlist = {"Numbers": Numbers, "People": People, "Family": Family}
allLev = [NotLearned,Learned,LevelOne,LevelTwo,Mastered]
all = [Numbers, People, Family]
charlist = []
set = random.choice(all)
k = set.keys()
k = random.sample(k, len(k))
outof = 0
total = len(set)
while True:
print "Welcome! Just type in the character requested! If you want to exit, type quit."
for x in range(0, len(set)):
char = k[x]
question = raw_input("Write Chinese for \"{}\" (Hint: it's {}): ".format(set[char], char))
if question == char:
print "You got it right!"
outof += 1
else:
print "Sorry, that's incorrect"
score = float(outof)/float(total)
print "You got {}%. Good Job!".format(100 * score)
if set in NotLearned:
print "Good job learning that set!"
NotLearned.remove(set)
Learned.append(set)
if set in Learned:
if score >= .5:
print "Good job practicing!"
Learned.remove(set)
Practiced.append(set)
if set in Practiced:
if score >= .6
Solution
First of all, this is a great idea!
Naming
Also, variable names in Python should use
The PEP8 command line tool can tell you these things, I suggest to give it a try!
Also,
Compatibility
It's ok if you have a reason to stay on Python 2.x, but it's still a good idea to make your scripts Python 3.x compatible when it's easy to do.
For example, you could replace
because the latter works in both versions, the former doesn't.
Bugs
The program has several bugs.
For example from the word set of People with 4 elements,
I "guessed" 3 correctly, got 75% score,
then in the next round I guessed 2 correctly so my score became 125% instead of 50%.
The fix is simple, move
Another bug is in the logic to level up.
In my previous example of getting 75% score,
this happened:
There's a problem with logic here.
Possibly you meant to use
Reducing the number of lists
I want to know if there is a better way to have fewer lists
The way you use the lists, is a bit strange...
From
words are sampled from this list,
and the list itself is moved through other lists: NotLearned -> Learned -> Practiced -> LevelOne -> ... and so on.
Instead of moving through NotLearned -> Learned -> ..., you could use a simple number to indicate the level, and increment as the user progresses.
Usability
Typing Chinese is problematic if you don't have a Chinese keyboard. I played the game by copy-pasting answers. It could be more interesting and more accessible to present multiple choices that can be selected by entering their numbers.
In each cycle of the main
See the
Unused variables
Eliminate all unused variables, they are just noise.
Naming
all and set are not good names, because they shadow Python built-in names. set is especially bad, because it actually contains a list, which is misleading.Also, variable names in Python should use
snake_case instead of CamelCase. For example "NotLearned" should be changed to not_learned.The PEP8 command line tool can tell you these things, I suggest to give it a try!
Also,
k is a very poor name for a random list of characters.Compatibility
It's ok if you have a reason to stay on Python 2.x, but it's still a good idea to make your scripts Python 3.x compatible when it's easy to do.
For example, you could replace
print something with print(something),because the latter works in both versions, the former doesn't.
Bugs
The program has several bugs.
outof is not reset at the beginning of each cycle.For example from the word set of People with 4 elements,
I "guessed" 3 correctly, got 75% score,
then in the next round I guessed 2 correctly so my score became 125% instead of 50%.
The fix is simple, move
outof = 0 inside the loop.Another bug is in the logic to level up.
In my previous example of getting 75% score,
this happened:
if set in NotLearned:
# => true => moving set from NotLearned to Learned
if set in Learned:
# => true, because the set was just moved to Learned
if score >= .5:
# => true, I scored 75%
# => moving set from Learned to Practiced
if set in Practiced:
# => true, because the set was just moved to Practiced
if score >= .6:
# => true, I scored 75%
# => moving set from Practiced to LevelOne
if set in LevelOne:
# => true, because the set was just moved to LevelOne
if score >= .75:
# => true, I scored 75%
# => moving set from Practiced to LevelOneThere's a problem with logic here.
Possibly you meant to use
elif instead of some (or all?) of those ifs.Reducing the number of lists
I want to know if there is a better way to have fewer lists
The way you use the lists, is a bit strange...
From
NotLearned, you randomly pick one of the lists (numbers, people or family), and that list will be used for the rest of the game:words are sampled from this list,
and the list itself is moved through other lists: NotLearned -> Learned -> Practiced -> LevelOne -> ... and so on.
Instead of moving through NotLearned -> Learned -> ..., you could use a simple number to indicate the level, and increment as the user progresses.
Usability
Typing Chinese is problematic if you don't have a Chinese keyboard. I played the game by copy-pasting answers. It could be more interesting and more accessible to present multiple choices that can be selected by entering their numbers.
In each cycle of the main
while loop, the words come in the same order. It would be more interesting if the order was randomized.See the
random.shuffle function.Unused variables
Eliminate all unused variables, they are just noise.
Code Snippets
if set in NotLearned:
# => true => moving set from NotLearned to Learned
if set in Learned:
# => true, because the set was just moved to Learned
if score >= .5:
# => true, I scored 75%
# => moving set from Learned to Practiced
if set in Practiced:
# => true, because the set was just moved to Practiced
if score >= .6:
# => true, I scored 75%
# => moving set from Practiced to LevelOne
if set in LevelOne:
# => true, because the set was just moved to LevelOne
if score >= .75:
# => true, I scored 75%
# => moving set from Practiced to LevelOneContext
StackExchange Code Review Q#128441, answer score: 5
Revisions (0)
No revisions yet.