Python Forum
Guessing Game with .WAV Files - Python Coding
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guessing Game with .WAV Files - Python Coding
#1
I've been working on a project but it appears I've gotten a tad... Stuck. I don't know how to code the guessing part. The random music playing is okay, I can do that. Can someone help me please?

import winsound
import time
import random
guess = 1
A7 = "H:\Guitar Chords\Aaug6.wav"
Amaj = "H:\Guitar Chords\Amaj.wav"
Amin = "H:\Guitar Chords\Amin.wav"
Amin7 = "H:\Guitar Chords\Amin7.wav"
Cmaj = "H:\Guitar Chords\Cmaj.wav"
D7 = "H:\Guitar Chords\D7.wav"
Dmaj = "H:\Guitar Chords\Dmaj.wav"
E7 = "H:\Guitar Chords\E7.wav"
Emaj = "H:\Guitar Chords\Emaj.wav"
Emin = "H:\Guitar Chords\Emin.wav"
Emin7 = "H:\Guitar Chords\Emin7.wav"
Gmaj = "H:\Guitar Chords\Gmaj.wav"
time.sleep(1.5)
while True:
        
    sounds = [A7, Amaj, Amin, Amin7, Cmaj, D7, Dmaj, E7, Emaj, Emin, Emin7, Gmaj]
    winsound.PlaySound(random.choice(sounds), winsound.SND_FILENAME|winsound.SND_ASYNC)
    
    
Reply
#2
First off, you'll need to ask the user what sound they thought it was. Then you'll take that response, and check it against the answer. As you've currently coded things, it would probably be tough to check the answer. What I would do to fix that would be to have sounds contain pairs (tuples) instead of just the path, where the tuples would contain the expected answer as well as the path.
Reply
#3
Okay so the checking response is easy, I can do basic number guessing games. You're right about the way I've coded it. Thank you. I shall figure out these tuple things!
Reply
#4
I've gotten stuck again. I've got no idea how to use tuples with the files. I have everything sorted but the comparing the response to what was actually played. I've even tried the dictionary function. I keep getting my "Try Again" even if the sound has been guessed correctly.

import winsound
import time
import random


A7 = "H:\Guitar Chords\Aaug6.wav"
Amaj = "H:\Guitar Chords\Amaj.wav"
Amin = "H:\Guitar Chords\Amin.wav"
Amin7 = "H:\Guitar Chords\Amin7.wav"
Cmaj = "H:\Guitar Chords\Cmaj.wav"
D7 = "H:\Guitar Chords\D7.wav"
Dmaj = "H:\Guitar Chords\Dmaj.wav"
E7 = "H:\Guitar Chords\E7.wav"
Emaj = "H:\Guitar Chords\Emaj.wav"
Emin = "H:\Guitar Chords\Emin.wav"
Emin7 = "H:\Guitar Chords\Emin7.wav"
Gmaj = "H:\Guitar Chords\Gmaj.wav"


while True:
    sound = (A7, Amaj, Amin, Amin7, Cmaj, D7, Dmaj, E7, Emaj, Emin, Emin7, Gmaj)
    s = {"A7": A7, "Amaj": Amaj, "Amin": Amin, "Amin7": Amin7, "Cmaj": Cmaj, "D7": D7, "Dmaj": Dmaj, "E7": E7, "Emaj": Emaj, "Emin": Emin, "Emin7": Emin7, "Gmaj": Gmaj}
    winsound.PlaySound(random.choice(sound), winsound.SND_FILENAME|winsound.SND_ASYNC)
    print("A7, Amaj, Amin, Amin7, Cmaj, D7, Dmaj, E7, Emaj, Emin, Emin7, Gmaj")
    guess = input("What sound was just played?: ")

    if guess == s:
        print("Well done!")
    else:
        print("Try again.")
This is what I have so far in my attempts.
Reply
#5
Actually, instead of tuples, you can use your dictionary. I'd replace your loop like so (untested)
# this only need to be defined once (not in the loop); I use sounds.keys() instead of a separate tuple
sounds = {"A7": A7, "Amaj": Amaj, "Amin": Amin, "Amin7": Amin7, "Cmaj": Cmaj, "D7": D7, "Dmaj": Dmaj, "E7": E7, "Emaj": Emaj, "Emin": Emin, "Emin7": Emin7, "Gmaj": Gmaj}

while True:
    choice = random.choice(sounds.keys())
    winsound.PlaySound(choice, winsound.SND_FILENAME|winsound.SND_ASYNC)
    print("A7, Amaj, Amin, Amin7, Cmaj, D7, Dmaj, E7, Emaj, Emin, Emin7, Gmaj")
    guess = input("What sound was just played?: ")
 
    if sounds.get(guess) == choice:
        print("Well done!")
    else:
        print("Try again.")
The alternative with tuples is a list of pairs instead of a dictionary. You'd use random.choice() on the list, and get a pair containing the sound path and the expected guess string. You could then use that. I think a list of tuples is slightly simpler, but it's up to you what you want to stick to.
Reply
#6
Okay. You can't is the sounds.keys() (I know it's untested just for future reference). I've done that in a different project. dict_keys doesn't support indexing. But this looks a lot... simpler than mine. I'll mess around and see if I can make this thing work.
Reply
#7
The simple solution is to replace
random.choice(sounds.keys())
with
random.choice(tuple(sounds.keys()))
(Or list instead of tuple.) To be more proper, rather than doing it in the loop though, I'd "cache" the keys and pull them out of the loop as a tuple.
Reply
#8
Quote:
random.choice(tuple(sounds.keys()))
(Or list instead of tuple.) To be more proper, rather than doing it in the loop though, I'd "cache" the keys and pull them out of the loop as a tuple.

I did list instead of tuple. Both ways however seem to... Play the windows sound. The sound of when you have a box open and have to deal with that before you click on anything else. Strange really.
Reply
#9
I fixed my issue. This is solved now. My code works fully. Also I changed .keys into .values and it works really well! Thank you for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Beginner Boolean question [Guessing game] TKB 4 2,330 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,925 Mar-20-2022, 08:16 PM
Last Post: menator01
  Tic-Tac game (Beginner's coding) Shahmadhur13 5 3,151 Aug-29-2020, 08:40 PM
Last Post: deanhystad
  Guessing game problem IcodeUser8 7 3,653 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,758 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Help - Guessing Game JamieT 5 2,783 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,184 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,299 Mar-09-2020, 04:53 PM
Last Post: snippsat
  guessing the number game go127a 6 4,860 Apr-27-2019, 01:23 PM
Last Post: go127a
  Guessing Game does not work the_entrepreneur 3 2,811 Apr-20-2019, 06:19 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020