Ich bin neu in der Programmierung. Es ist eine sehr persönliche Angelegenheit, aber ich kann es nicht tun. Könnten Sie mir also etwas Weisheit verleihen? Nachdem Sie das folgende Programm ausgeführt und das Spiel gespielt haben, wird der unten gezeigte Bildschirm angezeigt. Es heißt, WPM war 0.0, was für mich 30,40 sein sollte. Könnten Sie mir bitte sagen, was falsch ist und wo es behoben werden kann?
filename.rb
from tkinter import *
from tkinter.ttk import *
import random
window = Tk()
main_frame = Frame(window)
canvas = Canvas(window, width="400", height="300", background="#BDFFBA")
main_frame.grid(row=0, column=0)
time = 30
number_of_entries = 0
misstakes = 0
instruction = ""
words = open('words.txt')
word_list = words.read().split(", ")
def new_word():
global word_list
global number_of_entries
global misstakes
if time > 0:
user_entry.focus_set()
user_entry.delete(0, END)
random.shuffle(word_list)
typing_word.config(text=word_list[0])
number_of_entries_label.configure(text="number of entries: " + str(number_of_entries))
if user_entry.get().lower() == word_list[0]:
number_of_entries += 1
elif user_entry.get().lower() != word_list[0]:
misstakes += 1
def menu():
title_1.grid(row=0, column=0, pady=50)
play_button.grid(row=1, column=0, padx=100)
instruction_button.grid(row=2, column=0, pady=20)
exit_button.grid(row=3, column=0, pady=20)
time_label.grid_forget()
user_entry.grid_forget()
typing_word.grid_forget()
def start_game(event):
if time == 30:
time_change()
new_word()
def time_change():
global time
if time > 0:
time -= 1
time_label.config(text="Time: " + str(time))
time_label.after(1000, time_change)
if time == 0:
window.unbind("<Return>")
result_screen()
def play_pressed():
time_label.grid(row=0, column=0, pady=10)
user_entry.grid(row=2, column=0, pady=20)
typing_word.grid(row=1, column=0, pady=10)
window.bind( "<Return>", start_game)
title_1.grid_forget()
play_button.grid_forget()
instruction_button.grid_forget()
exit_button.grid_forget()
def instruction_pressed():
sub_win = Toplevel()
label1 = Label(sub_win, text=instruction)
label1.pack()
label2 = Label(sub_win, text=instruction)
label2.pack()
def restart_game_button_pressed():
global number_of_entries
global time
number_of_entries = 0
time = 30
play_pressed()
def result_screen():
restart_game_button.grid(pady=100, padx=100)
result_label.grid()
user_entry.grid_forget()
time_label.grid_forget()
typing_word.grid_forget()
characters_entries = number_of_entries * 8
gross_wpm = characters_entries / 0.5
gross_misstakes = misstakes / 0.5
WPM = gross_wpm - gross_misstakes
result = "Your WPM was {}".format(WPM)
result_label = Label(window, text=result)
result_label.configure(font=("Helvetica", 40))
s = Style()
s.configure("TButton", font=('Helvetica', 30))
title_1 = Label(main_frame, text="Prototype")
title_1.configure(font=("Helvetica", 44))
time_label = Label(window, text="Time: " + str(time))
time_label.configure(font=("Helvetica", 40))
number_of_entries_label = Label(window, text="WPM: " + str(number_of_entries))
user_entry = Entry(window, font=("Comic Sans MS", 20))
typing_word = Label(window, font=("Comic Sans MS", 15))
typing_word.configure(font=("Helvetica", 44))
play_button = Button(main_frame, text="Play", command=play_pressed, style="TButton")
instruction_button = Button(main_frame, text="How to play", command=instruction_pressed)
exit_button = Button(main_frame, text="EXIT", command=window.destroy)
restart_game_button = Button(main_frame, text="Play again", command=restart_game_button_pressed())
main_frame.grid(row=0, column=0)
menu()
window.mainloop()