OK, first, that is a little too simple for functions, just use if statements to check it. You need to add something like random turns to make it more reusable. Either that, or add a main menu and whatnot. Here is the basic game, then I will show you when you would use while True:
import random
def roller(player):
"""Pass in the player list and it will roll, print the roll, print the total and return the total."""
r = random.randint(1, 6)
player[1] += r
print("%s rolled %s. Now their score is %s/100" % (player[0], r, player[1]))
def change_turn(current_player, player_list):
"""This is called an iterator and it is a built-in object, but because we are just doing a basic alternator, this is probably better"""
if current_player[0] == player_list[0][0]:
return player_list[1]
else:
return player_list[0]
def main():
"""This is a nice little trick that will keep you from using global variables. Instead you are using the variables in the main function. Another trick is to use variables in another module which you will see later in Learn Python the Hard Way."""
player1 = ["Player1", 0]
player2 = ["Player2", 0]
player_list = [player1, player2]
#The next line chooses a random player to start
current_player = random.choice(player_list)
winner = None
print("Welcome to the player dice roler!\n%s is first:" % current_player[0])
while not winner: #What you were looking for is: while True: but frankly you only use that if you don't want the game to ever stop.
roller(current_player)
if current_player[1] >= 100:
winner = current_player
else:
current_player = change_turn(current_player, player_list)
print("%s: %s, %s: %s" % (player1[0], player1[1], player2[0], player2[1]))
raw_input("Press return to continue\n")
print("And the winner is: %s!!! With a final score of %s" % (winner[0], winner[1]))
main()
OK, so here is when you don't want your players to leave:
import random
def roller(player):
"""Pass in the player list and it will roll, print the roll, print the total and return the total."""
r = random.randint(1, 6)
player[1] += r
print("%s rolled %s. Now their score is %s/100" % (player[0], r, player[1]))
def change_turn(current_player, player_list):
"""This is called an iterator and it is a built-in object, but because we are just doing a basic alternator, this is probably better"""
if current_player[0] == player_list[0][0]:
return player_list[1]
else:
return player_list[0]
def game():
"""We just change the title here so it is not main which is used for the master function"""
player1 = ["Player1", 0]
player2 = ["Player2", 0]
player_list = [player1, player2]
#The next line chooses a random player to start
current_player = random.choice(player_list)
winner = None
print("Welcome to the player dice roler!\n%s is first:" % current_player[0])
while not winner: #What you were looking for is: while True: but frankly you only use that if you don't want the game to ever stop.
roller(current_player)
if current_player[1] >= 100:
winner = current_player
else:
current_player = change_turn(current_player, player_list)
print("%s: %s, %s: %s" % (player1[0], player1[1], player2[0], player2[1]))
raw_input("Press return to continue\n")
print("And the winner is: %s!!! With a final score of %s" % (winner[0], winner[1]))
def main():
"""Adds a main menu to the game"""
message = "What would you like to do?\n1. New Game!\n2. exit :("
print(message)
while True:
choice = raw_input("> ")
if choice == "1":
game()
print(message)
elif choice == "2":
break
main()
classes make things a whole lot nicer and so do dictionaries, but lists are the most basic. (*huge hint: an instance of a class, dictionary, list and module are pretty much all the same thing.)
Thus saying, can you change this so it uses a dict rather than a list, then when you start classes, can you make a player class rather than a dict?