mardi 31 juillet 2018

Python, random based code [on hold]

While writing the code encounter some problem, so I'm here again. Hope someone assists me a little. My problem right now, is that I want to buttons that show Chinese characters to shuffle or change places at random when button with pinyin (text) is pressed. I don't really need work done for me, mainly I am looking for suggestions and points for me.

#========== Imports ===========#

from tkinter import *
import random

#========== Parameters ==========#

CN = Tk()
CN.title("Chinese Test")
CNW = ["ài","bā","bàba","bēizi","Běijīng","běn","búkèqi","bù","cài","chá","chī","chūzūchē"]
CNI = ["爱","八","爸爸","杯子","北京","本","不客气","不","菜","茶","吃","出租车",
       "打电话","大","的","点","电脑","电视","电影","东西","都","读","对不起",
       "多","多少","儿子","二","饭店","飞机","分钟","高兴","个","工作","汉语",
       "好","号","喝","和","很","后面","回","会","几","家","叫","今天"]

#=========== Functions ==========#

def ButRandom():
    B0['text'] = random.choice(CNW)
    if (B0['text']=="ài"):
        B1['text'] = "爱"
        B2['text'] = random.choice(CNI)
        B3['text'] = random.choice(CNI)
        B4['text'] = random.choice(CNI)
    elif (B0['text']=="bā"):
        B1['text'] = "八"
        B2['text'] = random.choice(CNI)
        B3['text'] = random.choice(CNI)
        B4['text'] = random.choice(CNI)
    elif (B0['text']=="bàba"):
        B1['text'] = "爸爸"
        B2['text'] = random.choice(CNI)
        B3['text'] = random.choice(CNI)
        B4['text'] = random.choice(CNI)
    elif (B0['text']=="bēizi"):
        B1['text'] = "杯子"
        B2['text'] = random.choice(CNI)
        B3['text'] = random.choice(CNI)
        B4['text'] = random.choice(CNI)

# Test parameters

def ButCheck():
    B0['text'] = "Correct. Again?"
def ButCheck1():
    B0['text'] = "Incorrect"
def ButCheck2():
    B0['text'] = "Incorrect"
def ButCheck3():
    B0['text'] = "Incorrect"

#=========== Buttons ==========#

B0 = Button(CN, text = "Click to start", command = ButRandom, bd = 3)
B0.grid(row = 0, column = 1)

B1 = Button(CN, text = 1, command = ButCheck, bd = 3, width = 3, height = 2)
B1.grid(row = 2, column = 0)
B2 = Button(CN, text = 2, command = ButCheck1, bd = 3, width = 3, height = 2)
B2.grid(row = 2, column = 1)
B3 = Button(CN, text = 3, command = ButCheck2, bd = 3, width = 3, height = 2)
B3.grid(row = 2, column = 2)
B4 = Button(CN, text = 4, command = ButCheck3, bd = 3, width = 3, height = 2)
B4.grid(row = 2, column = 3)

#========== Pack ==========#

CN.mainloop(  )

I'm lack in basic knowledge, so it a little hard to me to find my possible mistake or optimized code.




Aucun commentaire:

Enregistrer un commentaire