How to Make a Game Play Again in Python
#one
How to program my game to ask 'Would yous like to play again?'
Posted 02 Baronial 2012 - 01:07 PM
Okay, so I've been coding a Tic Tac Toe game in Python 3.2.3. Then, I was curious, is at that place any way I can code my game at the end after the reckoner or person has i to ask the player if they want to play again? I've tried my best and googled everything, only a majority of them simply haven't worked out. And I was wondering if you guys could help me? I want to program my game to say like, "Would you like to play over again? Y or N:" and if the person types a Y, accept the game automatically restart and if they type a North, just accept the program quit and exit. Cheers and so much! Oh and hither's my lawmaking and then far:
import random import time board = [0,i,two, 3,4,5, vi,7,viii] def testify(): print (board[0],"|",board[1],"|",board[2]) print ("----------") impress (lath[3],"|",lath[4],"|",board[5]) impress ("----------") print (board[vi],"|",board[vii],"|",board[viii]) def checkLine(char, spot1, spot2, spot3): if board[spot1] == char and lath[spot2] == char and board[spot3] ==char: return True def checkAll(char): if checkLine(char, 0, 1, ii): render True if checkLine(char, ane, four, 7): return True if checkLine(char, two, 5, viii): return Truthful if checkLine(char, 6, 7, viii): render True if checkLine(char, iii, 4, 5): return True if checkLine(char, 1, 2, three): return True if checkLine(char, ii, four, six): return True if checkLine(char, 0, four, 8): return True while True: respond = input("Select a spot, any number 0 through 8:") respond = int(respond) fourth dimension.slumber(1) if lath[answer] != 'x' and lath[answer] !='o': board[answer] = '10' if checkAll('x') == True: print ("You win! Yay!") break; while Truthful: random.seed() opponent = random.randint(0,eight) if lath[opponent] != 'o' and board[opponent] != 'x': board[opponent] = 'o' if checkAll('o') == True: print ("You loose! That's to bad.") intermission; break; else: print ("This spot is taken!") show()
#2
Re: How to program my game to ask 'Would you like to play again?'
Posted 02 August 2012 - 01:12 PM
Information technology's pretty much just a practise-while or while loop. Right before your code exits ask that question.. read the input.. if they click 'y' then the loop repeats.. if 'n' and then the loop be and so does the program.
#3
Re: How to programme my game to ask 'Would you similar to play again?'
Posted 02 Baronial 2012 - 01:17 PM
modi123_1, on 02 Baronial 2012 - 01:12 PM, said:
It's pretty much only a do-while or while loop. Right earlier your code exits ask that question.. read the input.. if they click 'y' then the loop repeats.. if 'n' and so the loop exist and so does the programme.
Just what would the lawmaking be and where would it go?
#4
Re: How to program my game to enquire 'Would you like to play over again?'
Posted 02 August 2012 - 01:22 PM
Like I said... a Practise-WHILE loop would work... and it would wrap your existing code.. you would need to add two lines (one to ask to play once again and i to read in the user input) before the start of the condition of the Do-WHILE.
Loops are fairly basic things - I am certain yous can read upwards on them.
#5
Re: How to program my game to ask 'Would you like to play over again?'
Posted 02 August 2012 - 01:51 PM
information technology'south equally modi123_1 says. You lot already accept your game that can run in one case. Just put the whole of it in a function and create a primary loop that runs once more and again until the players says "N":
def game(): laissez passer #your game goes hither, whatsoever it is #main loop play_again = "Y" while play_again == "Y": game() play_again = input("do you lot want to play once more?(Y/Due north)")
This mail service has been edited by Nallo: 02 August 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/
Belum ada Komentar untuk "How to Make a Game Play Again in Python"
Posting Komentar