Sunday 15 April 2012

python - Using two counters in a loop -



python - Using two counters in a loop -

i have been making text based game @ home using python , have been having problem creating little section of combat between player , foe. combat supposed include 2 random rolls; 1 player , 1 alien. if alien rolls higher player +1 should added alien_wins counter , if player wins +1 should added player_wins counter.

what want happen when player_wins counter reaches 3 loop stop , produce message, same should happen when alien_ wins reaches 3 have had problem making work. help appreciated, thanks!

import random tkinter import * def fight(): player_wins = 0 alien_wins = 0 while player_wins <= 3: player_roll = random.randint(0, 10) alien_roll = random.randint(0, 7) if player_roll > alien_roll: contents.set("you manage fire shot of laser pistol , alien backs off.") player_wins += 1 homecoming elif alien_roll > player_roll: contents.set("the alien reaches out , strikes claws.") alien_wins += 1 homecoming elif alien_roll == player_roll: contents.set("you both grapple eachother , off.") homecoming if player_wins == 3: contents.set("you manage overcome alien. leaps wall wall , crawls vents.") win = true break elif alien_wins == 3: contents.set("you lose fighting alien. game over!") break base of operations = tk() contents = stringvar() display = message(base, textvariable = contents, relief=raised) display.pack() enterbutton = button(base, text = 'try again', command = fight).pack() base.mainloop()

you want go on fighting (looping) while either status (points <= 3) true. need add together status while loop.

you want homecoming function after fighting on remove of returns , add together 1 outside of loop after fighting over. also, want create messages after fighting over.

def fight(): player_wins = 0 alien_wins = 0 while player_wins <= 3 , alien_wins <= 3: player_roll = random.randint(0, 10) alien_roll = random.randint(0, 7) if player_roll > alien_roll: contents.set("you manage fire shot of laser pistol , alien backs off.") player_wins += 1 elif alien_roll > player_roll: contents.set("the alien reaches out , strikes claws.") alien_wins += 1 elif alien_roll == player_roll: contents.set("you both grapple eachother , off.") if player_wins == 3: contents.set("you manage overcome alien. leaps wall wall , crawls vents.") win = true elif alien_wins == 3: contents.set("you lose fighting alien. game over!") homecoming

python if-statement while-loop tkinter

No comments:

Post a Comment