Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

2 plater die game in python

import random

def get_name(key):
    n = ""
    while not n:
        n = input(f"Input your name, {key}: ")
    return n

def throw_dice(key):
    dices = random.choices(range(1,7),k=2)
    print(f"{names[key]} threw: {dices}", end = " ")
    if sum(dices) % 2 == 0:
        score[key] += 10
        print("Even. You earn 10 points.")
    else:
        score[key] -= 5
        print("Odd. You loose 5 points")

def print_score():
    for n in score:
        print(f"  {names[n]} has got {score[n]} points.")

def win_message(s,n):
    print_score()
    if score["P1"] > score["P2"]:
        print(f"{names['P1']} won")
    else:
        print(f"{names['P2']} won") 



player = ""
score = {}
names = {}

for n in ["P1","P2"]:
    names[n] = get_name(n)
    score[n] = 0

# twice the amount of rows wanted, because players take turn

for c in range(10):
    # switches between player1 and player2
    player = "P1" if player != "P1" else "P2"

    print(f"Round {c//2 + 1}: it is {names[player]}'s turn.")
    print_score()
    throw_dice(player)

win_message(score,names)
Comment

PREVIOUS NEXT
Code Example
Python :: python array linspace 
Python :: kivy stuck in fullscreen in jupyter notebook macbook 
Python :: /n python 
Python :: flask tutorial 
Python :: ascii to int python 
Python :: how to uninstall python-dotenv 
Python :: hide grid imshow 
Python :: access icloud doc on jupyter notebook 
Python :: check for changed model fields in djnago signal 
Python :: join on index python 
Python :: How to find the most similar word in a list in python 
Python :: dependency inversion 
Python :: py random.sample 
Python :: min stack in python 
Python :: pandas data frame from part of excel better example 
Python :: python requests cannot find existing url 
Python :: python create zip file 
Python :: keras functional api embedding layer 
Python :: how to interrupt a loop in python 
Python :: python listas por comprension 
Python :: Print characters from a string that are present at an even index number 
Python :: python remove table widget numbers 
Python :: How to build a Least Recently Used (LRU) cache, in Python? 
Python :: selenium session id python 
Python :: python integers 
Python :: numpy filter based on value 
Python :: list vs dictionary python 
Python :: pandas flip x and y axis 
Python :: python get function docstring 
Python :: python last non-zero value in a list 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =