O'Reilly Forums: Chapter 7 Gameshow.py While Loop Won't End - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 7 Gameshow.py While Loop Won't End sound won't play either

#1 User is offline   Christina Swenson 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 08-May 14

Posted 08 May 2014 - 11:57 AM

Here is the code (I copied it from the book)

I'm having 2 problems:

1) The sounds do not play. I have it in the folder with the sounds. Also, I did the previous program that tests sounds, and that does work. I even replaced the sounds in the working program with the "wrong.wav" and "correct.wav", they worked in the previous program but not this one.

2) When I run the program and put in 0, it won't leave the while loop to complete the program.

I have done and run both programs using TextWrangler (running in terminal) and IDLE

Same issues when I run them in either editor/shell

This post has been edited by Christina Swenson: 08 May 2014 - 12:06 PM

0

#2 User is offline   #TM# 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 62
  • Joined: 10-December 12
  • Gender:Not Telling

Posted 16 May 2014 - 06:23 AM

View PostChristina Swenson, on 08 May 2014 - 11:57 AM, said:

Here is the code (I copied it from the book)

I'm having 2 problems:

1) The sounds do not play. I have it in the folder with the sounds. Also, I did the previous program that tests sounds, and that does work. I even replaced the sounds in the working program with the "wrong.wav" and "correct.wav", they worked in the previous program but not this one.

2) When I run the program and put in 0, it won't leave the while loop to complete the program.

I have done and run both programs using TextWrangler (running in terminal) and IDLE

Same issues when I run them in either editor/shell



Sorry for the late response.

@Christina, I must say that it is not a proper way to send your code like the way you did.
People here at least paste their code in the forum. You have gone beyond that to make it a picture file !!! :(

You could have easily pasted your code between
 
[code]
paste your code here
[/code]
tags

or you can do it easily by clicking <> button on the forum's post editor.

For correcting a code someone has to copy paste your code and check and correct it thereafter.
Imagine if I will give you the corrected code in a picture format; how much painful and time consuming it will be to write it line by line in an editor and check it. :blink: . So, please ...

Though I won't do it. :P

Hope you will understand the problems in your program from the comment lines in the code

Your code:
import pygame # You don't need the whole pygame library so remove it
pygame.init() # You also don't need that
import pygame.mixer # Just this will be sufficient; will play sound perfectly

def wait_finish(channel):
	while channel.get_busy():
		pass

sounds = pygame.mixer
sounds.init()

correct_s = sounds.Sound("correct.wav")
wrong_s = sounds.Sound("wrong.wav")

prompt = "Press 1 for correct, 2 for wrong or 0 to quit : "

number_asked = 0
number_correct = 0
number_wrong = 0

choice = input(prompt)
while choice != '0':
	if choice == '1':
		number_asked = number_asked + 1
		number_correct = number_correct + 1
		wait_finish(correct_s.play())
	if choice == '2':
		number_asked = number_asked + 1
		number_correct = number_correct + 1 # every time you are increasing the 'number_correct' variable, so change it
		wait_finish(wrong_s.play())
	choice = input(prompt)
# Loop is stopping perfectly when the input is 0. 
# Precaution: Don't input 0 as '0' on output terminal.

print ("You asked " + str(number_asked) + " questions.")
print (str(number_correct) + " were correctly answered.")
print (str(number_wrong) + " were answered incorrectly.")



The corrected code:

import pygame.mixer

def wait_finish(channel):
	while channel.get_busy():
		pass

sounds = pygame.mixer
sounds.init()

correct_s = sounds.Sound("correct.wav")
wrong_s = sounds.Sound("wrong.wav")

prompt = "Press 1 for correct, 2 for wrong or 0 to quit : "

number_asked = 0
number_correct = 0
number_wrong = 0

choice = input(prompt)
while choice != '0':
	if choice == '1':
		number_asked += 1
		number_correct += 1
		wait_finish(correct_s.play())
	if choice == '2':
		number_asked += 1
		number_wrong += 1
		wait_finish(wrong_s.play())
	choice = input(prompt)

print ("You asked " + str(number_asked) + " questions.")
print (str(number_correct) + " were correctly answered.")
print (str(number_wrong) + " were answered incorrectly.")



Output (using Python 3.3 on windows 7, editor used - geany, sounds are playing) :

Press 1 for correct, 2 for wrong or 0 to quit : 1
Press 1 for correct, 2 for wrong or 0 to quit : 2
Press 1 for correct, 2 for wrong or 0 to quit : 1
Press 1 for correct, 2 for wrong or 0 to quit : 2
Press 1 for correct, 2 for wrong or 0 to quit : 1
Press 1 for correct, 2 for wrong or 0 to quit : 0
You asked 5 questions.
3 were correctly answered.
2 were answered incorrectly.
Press any key to continue . . .


Feel free to ask if something is not clear / program still not working.

B)

This post has been edited by #TM#: 16 May 2014 - 07:03 AM

0

#3 User is offline   Christina Swenson 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 08-May 14

Posted 16 May 2014 - 08:11 AM

Thank you for the advice on posting code here. Whenever I tried, it would take out my indentation and I knew that was an important aspect to the code. It was the first time I posted to this forum (or any forum, because I've usually been able to resolve my own issues for nearly 20 years). I did try multiple ways and in my frustration took the screen shot.

The lab I run is a MacLab, so we can't run pygame with 3.* version. I have to use 2.7, & our IT person (because I don't have admin permissions) configured it in a way where we do need to import pygame each time. He did try to follow the instructions that Head First gave to connect pygame to 3.* on Mac, but those didn't work.

The book has been good up to Chapter 7, but this chapter has caused the most frustration I have ever had to deal with running this lab in the last 8 years.

We finally resolved the loop issue by doing an if/elif/else loop instead, making while variable = True, then the else resets variable = False .

I turned the problem we had into a teaching moment by showing them the issue I was having, and asking them how else they might be able to resolve it. So far the if/elif/else is the best solution on the table if the while loop will not fall through to the next line of code.

Again, Thank you!
0

#4 User is offline   #TM# 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 62
  • Joined: 10-December 12
  • Gender:Not Telling

Posted 16 May 2014 - 09:17 AM

:)

This post has been edited by #TM#: 16 May 2014 - 09:19 AM

0

#5 User is offline   #TM# 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 62
  • Joined: 10-December 12
  • Gender:Not Telling

Posted 16 May 2014 - 09:19 AM

View PostChristina Swenson, on 16 May 2014 - 08:11 AM, said:

Thank you for the advice on posting code here. Whenever I tried, it would take out my indentation and I knew that was an important aspect to the code. It was the first time I posted to this forum (or any forum, because I've usually been able to resolve my own issues for nearly 20 years). I did try multiple ways and in my frustration took the screen shot.

The lab I run is a MacLab, so we can't run pygame with 3.* version. I have to use 2.7, & our IT person (because I don't have admin permissions) configured it in a way where we do need to import pygame each time. He did try to follow the instructions that Head First gave to connect pygame to 3.* on Mac, but those didn't work.

The book has been good up to Chapter 7, but this chapter has caused the most frustration I have ever had to deal with running this lab in the last 8 years.

We finally resolved the loop issue by doing an if/elif/else loop instead, making while variable = True, then the else resets variable = False .

I turned the problem we had into a teaching moment by showing them the issue I was having, and asking them how else they might be able to resolve it. So far the if/elif/else is the best solution on the table if the while loop will not fall through to the next line of code.

Again, Thank you!


@Christina ,
That's great ! You find another way to solve while loop issue. Programming is like that; new ways of doing things. B)

Seems to be a lot of trouble installing pygame for Mac. :blink:
I am not a Mac user. Hence no experience of installing pygame on it.

Although i have posted some solutions on the issue (after a thorough search on web) in the same forum thread previously. If you have missed that look at the below links. They may help.

You can try the below sites.

for python 2.7.x:

1. pygame, python 2.7.x for mac osx mountain lion

2.from pygame website (There is a bit clumsy instructions though)

I don't think pygame site has given any instructions for installing it for python 3.x.
But still I have got a tutorial after searching in google, you can try that.

for python 3.x:
1. pygame for python 3.x

(Problem with this one is you cannot install mercurial for python3; they don't support it. Skip that step and try to install. Probably it will work. Its probably the best tutorial on web to install pygame (python3) on Mac. Also, send this link to your IT admin; it will be of good help to him/her.

:)


This post has been edited by #TM#: 16 May 2014 - 09:26 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users