#๐ Player System and Addition to variables
40 messages ยท Page 1 of 1 (latest)
@stoic saffron
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.
thanks! was so confused lmao
# 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 ๐
what do you need help with?
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
you could do that 2 ways
4 variables that count the score
or
1 list that has all 4 scores in it
which would you recommend?
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
thats simplifies it a lot, wow! thanks :D
its bad code if you repeat yourself like that
there is always and easier and more scalable option
name = input(f"player {player+1} name: ")
currentPlayer.append(name)
How does this work?
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)```
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['luka']
wait thats so cool!
f"player {player+1} name: "
also I havent encounted text strings like this, how does this work?
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.
they are easier to write
instead of "text" + var + "text" + var
its f"text {var} text {var}"
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()
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
perfect, I will go try that out, thanks so much for the help! :D
!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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, 0, 14, 0]
you can use the same index for players and scores
player 1 will have the index 0 for both lists
alr, thank you so much for all the help, your a legend ๐ซก
no problem
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.