0

The Python shell is showing me this error

Traceback (most recent call last):
  File "C:\Users\DELL\Desktop\winsound.py", line 1, in <module>
    import winsound
  File "C:\Users\DELL\Desktop\winsound.py", line 5, in <module>
    winsound.beep(frequency,duration)
AttributeError: 'module' object has no attribute 'beep'

Code in winsound.py

import winsound 
from random import randrange 

frequency = randrange(5000) 
duration = randrange (2000) 
winsound.beep(frequency,duration)
gelonida
  • 5,327
  • 2
  • 23
  • 41
  • 1
    Can you provide your code? – moosehead42 Jul 15 '20 at 09:08
  • We cannot tell you what is wrong with your code unless you show us your code. – Karl Knechtel Jul 15 '20 at 09:09
  • 1
    Try a capital "B" winsound.Beep ; cf https://docs.python.org/2/library/winsound.html. – Demi-Lune Jul 15 '20 at 09:09
  • 4
    Actually, wait, never mind. The problem is that you tried to call your own source code `winsound.py`, which means it tries to import itself, instead of the standard library module. Use a different name. – Karl Knechtel Jul 15 '20 at 09:10
  • This is the code-- import winsound from random import randrange frequency = randrange(5000) duration = randrange (2000) winsound.beep(frequency,duration) – yasir shaukat Jul 15 '20 at 09:27
  • Please note, that it is better to add the code to your question. comments don't allow formatting and may be deleted lateron. I will do it for you now. Please, nest time do it yourself. – gelonida Jul 15 '20 at 09:51
  • as @KarlKnechtel said it's also important to specify how the python file is called, that contains your code as the filename `"winsound.py"` would be a breaking filename if you try to import `winsound` – gelonida Jul 15 '20 at 09:57
  • 1
    Does this answer your question? [Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name”](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – MisterMiyagi Jul 15 '20 at 10:58

1 Answers1

1

Below is the documentation of the winsound.Beep

winsound.Beep(frequency, duration)

Beep the PC’s speaker. The frequency parameter specifies frequency, in hertz, of the sound, and must be in the range 37 through 32,767. The duration parameter specifies the number of milliseconds the sound should last. If the system is not able to beep the speaker, RuntimeError is raised.

Try changing beep to Beep. it will solve your problem