0
from tkinter import *
import random
from collections import Counter


root = Tk()
root.title("Random")
root.geometry("600x400")
root.resizable(False, False)


def open_gn():
    gn_wn = Tk()
    gn_wn.title("Random App - Generate a number")
    gn_wn.geometry("600x400")
    gn_wn.resizable(False, False)

    Label(gn_wn, text='                                                          ').grid(row=0, column=0)
    inst_gn = Label(gn_wn, text='Enter a minimum and maximum value')
    inst_gn.config(font=("Yu Gothic UI", 12))
    inst_gn.grid(row=0, column=1)

    Label(gn_wn, text=" Enter a minimum value: ").place(x=295, y=100)
    entry_min = Entry(gn_wn)
    entry_min.place(x=450, y=100)

    Label(gn_wn, text=" Enter a maximum value: ").place(x=295, y=200)
    entry_max = Entry(gn_wn)
    entry_max.place(x=450, y=200)

    Label(gn_wn, text="Random value is: ").place(x=40, y=40)

    def generate_number():
        min_ = int(entry_min.get())
        max_ = int(entry_max.get())

        random_num = random.randint(min_, max_)
        d_rn = Label(gn_wn, text=random_num)
        d_rn.config(font=("Yu Gothic UI", 14))
        d_rn.place(x=40, y=80)

    Button(gn_wn, text="Generate", padx=220, pady=25, command=generate_number).place(x=25, y=280)

    gn_wn.mainloop()


def open_coin():
    c_wn = Tk()
    c_wn.title("Random App - Flip a Coin")
    c_wn.geometry("600x400")
    c_wn.resizable(False, False)

    Label(c_wn, text="                                                                                ").grid(row=0,
                                                                                                              column=0)
    Label(c_wn, text="Flip the coin below!", font=("Yu Gothic UI", 12)).grid(row=0, column=1)

    Label(c_wn, text='                    ').grid(row=1, column=1)

    coin_label = Label(c_wn, text="")
    coin_label.place(relx=0.5, rely=0.2, anchor='s')

    def flip():
        coin_values = ["Heads", "Tails"]
        coin_face = random.choice(coin_values)
        if coin_face == "Heads":
            coin_label.config(text="Coin: Heads")
        else:
            coin_label.config(text="Coin: Tails")

    coin = Button(c_wn, text='coin', padx=100, pady=90, command=flip)
    coin.place(relx=0.5, rely=0.5, anchor=CENTER)

    c_wn.mainloop()


def open_average():
    avg_wn = Tk()
    avg_wn.title("Random App - Averages")
    avg_wn.geometry("840x300")

    Label(avg_wn, text="              ").grid(row=0, column=0)
    avg_instruct = Label(avg_wn, text="Enter your values below to get the averages in mean, median, and mode(put a "
                                      "space between commas")
    avg_instruct.config(font=("Yu Gothic UI", 10))
    avg_instruct.grid(row=0, column=1)

    Label(avg_wn, text="                                     ").grid(row=1, column=0)
    entry = Entry(avg_wn)
    entry.grid(row=2, column=1)

    def calculate():
        list_data = entry.get().split(', ')
        list_data = [float(i) for i in list_data]
        mean = sum(list_data) / len(list_data)
        Label(avg_wn, text='Mean').grid(row=5, column=0)
        Label(avg_wn, text=str(mean)).grid(row=6, column=0)

        list_data_len = len(list_data)
        list_data.sort()

        if list_data_len % 2 == 0:
            median1 = list_data[list_data_len // 2]
            median2 = list_data[list_data_len // 2 - 1]
            median = (median1 + median2) / 2
        else:
            median = list_data[list_data_len // 2]
        Label(avg_wn, text='Median: ').grid(row=5, column=1)
        Label(avg_wn, text=median).grid(row=6, column=1)
        list_data_for_mode = Counter(list_data)
        get_mode = dict(list_data_for_mode)
        mode = [k for k, v in get_mode.items() if v == max(list(list_data_for_mode.values()))]

        if len(mode) == list_data_len:
            get_mode = ["No mode found"]
        else:
            get_mode = [str(i) for i in mode]

        Label(avg_wn, text="Mode: ").grid(row=5, column=2)
        Label(avg_wn, text=get_mode[0]).grid(row=6, column=2)

    Label(avg_wn, text="                                     ").grid(row=3, column=0)

    Button(avg_wn, text='Enter', command=calculate).grid(row=4, column=1)


Label(root, text="                                                          ").grid(row=0, column=0)

title = Label(root, text="Welcome to Random")
title.config(font=("Yu Gothic UI", 24))
title.grid(row=0, column=1)

button1 = Button(root, text="Generate a random number", padx=80, pady=25, command=open_gn)
button1.place(x=2.25, y=100)

button2 = Button(root, text="Calculate mean, mode, median, and range", padx=20, pady=25, command=open_average)
button2.place(x=325, y=100)

button3 = Button(root, text="Flip a Coin", padx=125, pady=25, command=open_coin)
button3.place(x=2.25, y=200)

button4 = Button(root, text="Create a graph to analyze", padx=66, pady=25)
button4.place(x=325, y=200)

root.mainloop()

I am trying to make an application that can do a bunch of statistics-related stuff and in it, I am trying to make a function that can generate a random number(go to the def open_gn() part. Thats where the error is.) However, when I try to run it, the program returns an error saying:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\redde\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Learn_python (2)/random app/main.py", line 37, in generate_number
    random_num = random.randint(min_, max_)
AttributeError: module 'random' has no attribute 'randint'

I tried copying and pasting the code where I use the randint attribute but it also recieved an error. Please help.

DRK CYAN
  • 21
  • 6
  • Try cutting your program to the minimum code that shows this error. That will make it easier for you to debug, and easier for us to help you. Once you have fixed the error you can add all the rest back in. – rossum Mar 13 '22 at 08:58

1 Answers1

0

Did you happen to name your Python script random.py? This thread suggests that that might be the cause of your issue. If it is, rename the script to something that is not a keyword and try again (for example, random_app.py).

  • "something that is not a keyword" - So for example call it `random`? – Kelly Bundy Mar 12 '22 at 22:30
  • @KellyBundy I should rephrase, I used the wrong term: the script should be renamed to something that does not conflict with the name of the imported module. – HiddenMachine Mar 12 '22 at 22:32
  • I have a feeling that's not the problem because of `File "C:/Learn_python (2)/random app/main.py", line 37, in generate_number`. Even so, I can't reproduce this on my computer. – QWERTYL Mar 12 '22 at 22:34
  • No, my script does not have the name random. If you want I can show you a video of me getting the error message for more details. – DRK CYAN Mar 12 '22 at 22:36
  • @QWERTYL If I name the script `random_app.py`, there are no issues. When I name the script `random.py`, I get an identical error to the one OP mentioned when running the random integer generation portion of the script. I'm quite sure that the script name is the problem. – HiddenMachine Mar 12 '22 at 22:37
  • @DRKCYAN Do you have any other scripts in the same folder with that name? – HiddenMachine Mar 12 '22 at 22:37
  • @HiddenMachine I agree that if his file was named random it would be a problem, but from the error message it seems it's not named random – QWERTYL Mar 12 '22 at 22:37
  • @QWERTYL My apologies, I see what you meant about the error message now. – HiddenMachine Mar 12 '22 at 22:39
  • According to the following link, it could be caused by an unrelated file named random.py in the same directory actually: https://www.reddit.com/r/learnpython/comments/9fr71g/module_object_has_no_attribute_randint_help/ – QWERTYL Mar 12 '22 at 22:40
  • Nope. I had a directory inside of my directory called "random app" but even when i changed it, it did not work. – DRK CYAN Mar 12 '22 at 22:41
  • 1
    @DRKCYAN What does `print(random)` say? – Kelly Bundy Mar 12 '22 at 22:42
  • @DRKCYAN I would recommend trying a suggestion from the linked Reddit thread to print the value of `random.__file__` just to see what's going on. – HiddenMachine Mar 12 '22 at 22:44
  • Ok i did but what did it do. It returned: C:\Users\redde\AppData\Local\Programs\Python\Python38-32\lib\random.py Does this mean I do have a file named random and if so how to fix it – DRK CYAN Mar 13 '22 at 01:20
  • That looks alright, it's Python's own `random.py`, not one you wrote. What does `print(dir(random))` show? – Kelly Bundy Mar 13 '22 at 01:35
  • ok apparently i did have a file called random. i renamed it now and it works – DRK CYAN Mar 16 '22 at 18:24