8 Replies - 3147 Views - Last Post: 27 July 2011 - 01:39 PM Rate Topic: -----

#1 EB547  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-February 09

Restarting a program in python

Posted 27 July 2011 - 10:59 AM

I'm new to python, and i have a problem with my first program, where i am trying hard to get it to loop back to the original doing statement, which i cannot seem to do, any ideas?

import getpass
import os
import sys

doing = True
while doing:
	bounce = False
	number = int(raw_input("Player 1, enter your number here: "))
	os.system('cls')
	running = True #Main Routine
	while running:
		guess = int(raw_input("Player 2, Guess: "))
		
		if guess == number:
			print "You Guessed It! Congrats!"
			running = False
			bounce = True
			doing = False
		
		elif guess < number:
			print "No, its higher than that."
		
		elif guess > number:
			print "No, its lower than that."
		
		else:
			print "Error With Game"
			running = False
			Bounce = False
			doing = False
			
while bounce:
	re = raw_input("Want to play again? (Yes/No) ")
	
	if re == "Yes":
		bounce = False
	else:
		print "Game Over"
		break
doing = True



Is This A Good Question/Topic? 0
  • +

Replies To: Restarting a program in python

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Restarting a program in python

Posted 27 July 2011 - 11:04 AM

Settings 'doing = True' at the bottom has no effect because the doing loop has already run. What I recommend you do is break this up into functions. Have a main() function that contains what is currently the 'while bounce' loop, and have another function named game() or doing() or whatever you want. Edit the program so that if the player answers yes when asked if they'd like to play again, you call game() (or whatever you named the function).
Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Restarting a program in python

Posted 27 July 2011 - 11:05 AM

After line 35, you will need to set doing = true. Then you will need to indent the entire while bounce: to be inside the while doing: loop.

This post has been edited by Motoma: 27 July 2011 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 EB547  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-February 09

Re: Restarting a program in python

Posted 27 July 2011 - 11:23 AM

View PostMotoma, on 27 July 2011 - 12:05 PM, said:

After line 35, you will need to set doing = true. Then you will need to indent the entire while bounce: to be inside the while doing: loop.


Tried it, couldnt get it to work...

View PostBrewer, on 27 July 2011 - 12:04 PM, said:

Settings 'doing = True' at the bottom has no effect because the doing loop has already run. What I recommend you do is break this up into functions. Have a main() function that contains what is currently the 'while bounce' loop, and have another function named game() or doing() or whatever you want. Edit the program so that if the player answers yes when asked if they'd like to play again, you call game() (or whatever you named the function).


Also tried, again no luck.
I am severely inexperienced, and this is a weird problem I've never experienced.
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Restarting a program in python

Posted 27 July 2011 - 11:23 AM

View PostEB547, on 27 July 2011 - 02:20 PM, said:

View PostMotoma, on 27 July 2011 - 12:05 PM, said:

After line 35, you will need to set doing = true. Then you will need to indent the entire while bounce: to be inside the while doing: loop.


Tried it, couldnt get it to work...


Let's take a look; please post your current code.
Was This Post Helpful? 0
  • +
  • -

#6 EB547  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-February 09

Re: Restarting a program in python

Posted 27 July 2011 - 11:28 AM

import getpass
import os
import sys

doing = True
while doing:
	bounce = False
	number = int(raw_input("Player 1, enter your number here: "))
	os.system('cls')
	running = True #Main Routine
	while running:
		guess = int(raw_input("Player 2, Guess: "))
		
		if guess == number:
			print "You Guessed It! Congrats!"
			running = False
			bounce = True
			doing = False
		
		elif guess < number:
			print "No, its higher than that."
		
		elif guess > number:
			print "No, its lower than that."
		
		else:
			print "Error With Game"
			running = False
			Bounce = False
			doing = False
	while bounce:
		re = raw_input("Want to play again? (Yes/No) ")
		if re == "Yes":
			bounce = False
			doing = True
	else:
		print "Game Over"
		break
	

Was This Post Helpful? 0
  • +
  • -

#7 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Restarting a program in python

Posted 27 July 2011 - 11:46 AM

What is bounce supposed to signify? The code works just fine without it:

import getpass
import os
import sys

doing = True
while doing:
	number = int(raw_input("Player 1, enter your number here: "))
	os.system('cls')

	running = True #Main Routine
	while running:
		guess = int(raw_input("Player 2, Guess: "))
		
		if guess == number:
			print "You Guessed It! Congrats!"
			running = False
		
		elif guess < number:
			print "No, its higher than that."
		
		elif guess > number:
			print "No, its lower than that."
		
		else:
			print "Error With Game"
			running = False
			doing = False

	re = raw_input("Want to play again? (Yes/No) ")
	if re == "Yes":
		doing = True
	else:
		print "Game Over"
		doing = false


Was This Post Helpful? 1
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Restarting a program in python

Posted 27 July 2011 - 12:05 PM

Buy some functions. They'll make your game loops far easier to follow.

e.g.
from getpass import getpass
import os
import sys

def playerGuess(number):
	running = True #Main Routine
	while running:
		guess = int(raw_input("Player 2, Guess: "))
		
		if guess == number:
			print "You Guessed It! Congrats!"
			running = False
		elif guess < number:
			print "No, its higher than that."
		elif guess > number:
			print "No, its lower than that."

def play():
	number = int(getpass("Player 1, enter your number here: "))
	playerGuess(number)


while True:
	play()
	re = raw_input("Want to play again? (Yes/No) ")
	if not re == "Yes":
		print "Game Over"
		break


Was This Post Helpful? 4
  • +
  • -

#9 EB547  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-February 09

Re: Restarting a program in python

Posted 27 July 2011 - 01:39 PM

Thanks to all, you guys got it working :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1