Prompt User to Play Again Python
#1
How to program my game to ask 'Would you like to play again?'
Posted 02 August 2012 - 01:07 PM
Okay, and then I've been coding a Tic Tac Toe game in Python 3.2.3. So, I was curious, is there any way I can code my game at the terminate later the computer or person has ane to enquire the player if they want to play once more? I've tried my best and googled everything, but a majority of them just haven't worked out. And I was wondering if you guys could assistance me? I want to plan my game to say like, "Would you like to play once more? Y or N:" and if the person types a Y, have the game automatically restart and if they type a Northward, only have the program quit and get out. Cheers then much! Oh and here's my code so far:
import random import time board = [0,1,2, 3,four,5, vi,vii,8] def show(): print (board[0],"|",board[1],"|",lath[two]) print ("----------") impress (lath[3],"|",board[iv],"|",board[5]) print ("----------") print (board[vi],"|",board[7],"|",lath[8]) def checkLine(char, spot1, spot2, spot3): if board[spot1] == char and lath[spot2] == char and board[spot3] ==char: render True def checkAll(char): if checkLine(char, 0, ane, ii): return True if checkLine(char, 1, 4, 7): return True if checkLine(char, 2, 5, 8): return True if checkLine(char, 6, 7, viii): render Truthful if checkLine(char, iii, 4, v): render True if checkLine(char, 1, 2, 3): return True if checkLine(char, ii, iv, half-dozen): return Truthful if checkLine(char, 0, 4, 8): return True while True: respond = input("Select a spot, any number 0 through viii:") reply = int(respond) fourth dimension.sleep(1) if lath[answer] != 'x' and lath[respond] !='o': board[answer] = 'x' if checkAll('x') == True: impress ("You win! Yay!") break; while True: random.seed() opponent = random.randint(0,8) if board[opponent] != 'o' and board[opponent] != 'x': board[opponent] = 'o' if checkAll('o') == True: print ("You lot loose! That's to bad.") break; interruption; else: print ("This spot is taken!") show()
#2
Re: How to programme my game to inquire 'Would y'all like to play again?'
Posted 02 August 2012 - 01:12 PM
Information technology's pretty much just a do-while or while loop. Right earlier your code exits ask that question.. read the input.. if they click 'y' so the loop repeats.. if 'n' then the loop exist and then does the program.
#3
Re: How to program my game to ask 'Would you like to play once again?'
Posted 02 August 2012 - 01:17 PM
modi123_1, on 02 Baronial 2012 - 01:12 PM, said:
It's pretty much simply a do-while or while loop. Right before your lawmaking exits inquire that question.. read the input.. if they click 'y' so the loop repeats.. if 'n' and then the loop exist and so does the plan.
But what would the lawmaking be and where would information technology go?
#4
Re: How to programme my game to ask 'Would you like to play again?'
Posted 02 August 2012 - 01:22 PM
Like I said... a Do-WHILE loop would work... and it would wrap your existing code.. you would demand to add two lines (one to enquire to play again and i to read in the user input) before the get-go of the condition of the Practise-WHILE.
Loops are adequately bones things - I am certain you can read up on them.
#5
Re: How to plan my game to ask 'Would you like to play over again?'
Posted 02 August 2012 - 01:51 PM
information technology's as modi123_1 says. You lot already have your game that tin run once. Just put the whole of information technology in a function and create a main loop that runs once more and again until the players says "Northward":
def game(): pass #your game goes here, any information technology is #primary loop play_again = "Y" while play_again == "Y": game() play_again = input("do you lot want to play again?(Y/N)")
This postal service has been edited by Nallo: 02 Baronial 2012 - 01:52 PM
Source: https://www.dreamincode.net/forums/topic/287733-how-to-program-my-game-to-ask-would-you-like-to-play-again/
0 Response to "Prompt User to Play Again Python"
Post a Comment