dimanche 24 avril 2022

Defining a function that will interpret whether or not an answer has been input and give out a random string from a list

So here is the code in question. The error I get when I run the code is File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\random.py", line 259, in choice

TypeError: object of type 'type' has no len()

import random
import tkinter as tk
from tkinter import messagebox

root=tk.Tk()
root.title("Tragic 8 Ball")

def get_answer(entry, list):
    if (entry.get() != ""):
        messagebox.showwarning("Please ask question.")
    else: (entry.get() == "")
    messagebox.showwarning("Your answer", random.choice(list))
    
entry=tk.Entry(width=40)
entry.focus_set()
entry.grid()

get_answer(entry, list)

tk.Label(root, text="Ask a question:").grid(row=0)
tk.Button(root, text="Answer my question", command=get_answer(entry, list).grid(row=3), columd=0, sticky=tk.W, pady=4)

list=["It is certain.",
      "Outlook good.",
      "You may rely on it",
      "Ask again later.",
      "Concentrate and ask again.",
      "Reply hazy, try again.",
      "My reply is no.",
      "My sources say no."]

root.mainloop()
'''



Aucun commentaire:

Enregistrer un commentaire