#๐Ÿ”’ Player System and Addition to variables

40 messages ยท Page 1 of 1 (latest)

lucid bronzeBOT
#

@stoic saffron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

heavy sapphire
#

you cant paste files here

#

!code

lucid bronzeBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

stoic saffron
#
# PYTHON DICE GAME

# Imports, Variables & Functions

import time
import random

def Roll() :

    rollTimes = 6

    while rollTimes >= 1 :
        global roll
        roll = random.randint(1, 6)
        print("Rolling Dice:    ", end='\r')
        time.sleep(0.15)
        print("Rolling Dice: ", roll, end='\r')
        time.sleep(0.65)
        rollTimes = rollTimes - 1

    print("                ", end='\r')
    print("You rolled a", roll)

    time.sleep(1)

print("Python Dice Game")
print("------ ---- ----")

# Player System

numPlayers = int(input("How many players (max 4): "))
print("There will be", numPlayers, "playing.")
choiceA = input("Is this correct? (y/n) ")
print("")

while choiceA != 'y' or numPlayers > 4:
    numPlayers = int(input("How many players (max 4): "))
    print("There will be", numPlayers, "playing.")
    choiceA = input("Is this correct? (y/n) ")
    print("")


while numPlayers == 1 :
    one = input("Player one, enter a username: ")
    currentPlayer = [one]
    break

while numPlayers == 2 :
    one = input("Player one, enter a username: ")
    two = input("Player two, enter a username: ")
    currentPlayer = [one, two]
    break

while numPlayers == 3 :
    one = input("Player one, enter a username: ")
    two = input("Player two, enter a username: ")
    three = input("Player three, enter a username: ")
    currentPlayer = [one, two, three]
    break

while numPlayers == 4 :
    one = input("Player one, enter a username: ")
    two = input("Player two, enter a username: ")
    three = input("Player three, enter a username: ")
    four = input("Player four, enter a username: ")
    currentPlayer = [one, two, three, four]
    break

# Game

turns = int(input("How many turns? "))
print("")

while turns > 0 :
    turns -= 1
    for index in currentPlayer :
        print(index, "its your turn to roll!")
        Roll()
#

oh man this is confusing ๐Ÿ˜‚

heavy sapphire
#

what do you need help with?

stoic saffron
#

I'm trying to make it so as it cycles through players it stores a total number of dice scores in a variable for each player

heavy sapphire
#

you could do that 2 ways
4 variables that count the score
or
1 list that has all 4 scores in it

stoic saffron
#

which would you recommend?

heavy sapphire
#

i would use the list

#

that way its only 1 variable

#

also this can be simplified a lot

#
players = 4
currentPlayer = []
for player in range(0, players):
    name = input(f"player {player+1} name: ")
    currentPlayer.append(name)```
something like this would allow you to have n amount of players
stoic saffron
heavy sapphire
#

its bad code if you repeat yourself like that
there is always and easier and more scalable option

stoic saffron
#
name = input(f"player {player+1} name: ")
currentPlayer.append(name)

How does this work?

heavy sapphire
#

that would append the name to the list
so if you enter the name luka it would get added to the list

#

!e

names = []
names.append("luka")
print(names)```
lucid bronzeBOT
stoic saffron
#

wait thats so cool!

#

f"player {player+1} name: "

also I havent encounted text strings like this, how does this work?

heavy sapphire
#

these are called f-strings

#

!f-string

lucid bronzeBOT
#
Format-strings

Creating a Python string with your variables using the + operator can be difficult to write and read. F-strings (format-strings) make it easy to insert values into a string. If you put an f in front of the first quote, you can then put Python expressions between curly braces in the string.

>>> snake = "pythons"
>>> number = 21
>>> f"There are {number * 2} {snake} on the plane."
"There are 42 pythons on the plane."

Note that even when you include an expression that isn't a string, like number * 2, Python will convert it to a string for you.

heavy sapphire
#

they are easier to write
instead of "text" + var + "text" + var
its f"text {var} text {var}"

stoic saffron
#

ok thanks! :)

#

but how can I add the value of the dice to a total score per player here:

# Game

turns = int(input("How many turns? "))
print("")

while turns > 0 :
    turns -= 1
    for index in currentPlayer :
        print(index, "its your turn to roll!")
        Roll()
heavy sapphire
#

i would have a list called scores
that list would have the length of the player count
then i would use the indexes of each player to put data inside the list

stoic saffron
heavy sapphire
#

!epy players = 4 scores = [] for player in range(0, players): scores.append(0) #set everyting to 0 in the list scores[2] = 14 print(scores)

lucid bronzeBOT
heavy sapphire
#

you can use the same index for players and scores
player 1 will have the index 0 for both lists

stoic saffron
#

alr, thank you so much for all the help, your a legend ๐Ÿซก

heavy sapphire
#

no problem

stoic saffron
#

!resolved

#

!close

lucid bronzeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.