繁体   English   中英

从 Python 中的列表中删除内容

[英]Removing things from lists in Python

当我运行这个程序时,我希望它删除已选择的任何数字,而不是让它在下一次循环代码时重新出现,但是当它第二次循环代码时,它完全忘记了删除数字从第一次开始。 请帮忙(对不起,我不擅长解释)

有问题的部分

while Play_game == True:

# play the game

colour = ["Red", "Yellow", "Black"]

# create the colour and number for player 1
number_R_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_Y_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_B_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
colour_p_1 = random.choice(colour)

if colour_p_1 == "Red":
    number_p_1 = random.choice(number_R_1)
    number_R_1.remove(number_p_1)

elif colour_p_1 == "Yellow":
    number_p_1 = random.choice(number_Y_1)
    number_Y_1.remove(number_p_1)

elif colour_p_1 == "Black":
    number_p_1 = random.choice(number_B_1)
    number_B_1.remove(number_p_1)

# create the colour and number for player 2

colour_p_2 = random.choice(colour)

number_R_2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_Y_2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_B_2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

if colour_p_2 == "Red":
    number_p_2 = random.choice(number_R_2)
    number_R_2.remove(number_p_2)

elif colour_p_2 == "Yellow":
    number_p_2 = random.choice(number_Y_2)
    number_Y_2.remove(number_p_2)

elif colour_p_2 == "Black":
    number_p_2 = random.choice(number_B_2)
    number_B_2.remove(number_p_2)

input("{0} press enter to pick up a card".format(user_1))
print("{0}'s draw : ".format(user_1))
player_1_colour = print(colour_p_1)
player_1_number = print(number_p_1)

input("{0} press enter to pick up a card".format(user_2))
print("{0}'s draw : ".format(user_2))
player_2_colour = print(colour_p_2)
player_2_number = print(number_p_2)

完整代码

# Card Game

# modules needed
import random
import time

# introduce the players to the game and the rules


Rules = True
Authenticate = False
Play_game = False

while Rules == True:
print("Hello and welcome to the Card Game, in this game there are 3 rules.")
time.sleep(4)
print("Rule number one: Each player takes their turn to take a card from the draw pile")
time.sleep(5)
print("Rule number two: There are 3 different coloured cards you could have collected as well as "
      "30 different numbers,if both players gain the same colour who ever has the highest number wins "
      "if you don't have the same colour then this is how it will work:")
time.sleep(10)
print(" ")
print("card  | card  | winner")
print("----------------------")
print("Red   | Black | Red")
print("----------------------")
print("Yellow|  Red  | Yellow")
print("----------------------")
print("Black | Yellow| Black")
print(" ")
print("Rule number three: You will play until there are no cards left in the deck, who ever has the most 
cards win, good luck!")
print(" ")

#give the user an option of play, rules or exit

options = input("Would you like to play, exit or read the rules again? (type play, exit, rules) : ")

if options == "rules" or options == "Rules":
    Rules = True
elif options == "exit" or options == "Exit":
    exit()

elif options == "play" or options == "Play":
    Rules = False
    Authenticate = True

else:
    print("Sorry this was not an option (you can only choose play, exit or rules)")

#Get the players names
user_1 = input("Before we begin, what is your name user 1? :")
print("You have a very nice name {0}".format(user_1))
user_2 = input("What is your name user 2? :")
print("you also have a very nice name {0}".format(user_2))

while Authenticate == True:

#authenticate the user

Authentication = input("Have you already created an account? (Yes or No) : ")
if Authentication == "yes" or Authentication == "Yes":
    Play_game = True
    Authenticate = False
elif Authentication == "no" or Authentication == "No":
    print("sorry this isnt completed yet please type Yes")
else:
    print("This was not an option the only options were Yes or No!")


number_R_2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_Y_2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_B_2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

while Play_game == True:

# play the game

colour = ["Red", "Yellow", "Black"]

# create the colour and number for player 1
number_R_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_Y_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
number_B_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
colour_p_1 = random.choice(colour)

if colour_p_1 == "Red":
    number_p_1 = random.choice(number_R_1)
    number_R_1.remove(number_p_1)

elif colour_p_1 == "Yellow":
    number_p_1 = random.choice(number_Y_1)
    number_Y_1.remove(number_p_1)

elif colour_p_1 == "Black":
    number_p_1 = random.choice(number_B_1)
    number_B_1.remove(number_p_1)

# create the colour and number for player 2

colour_p_2 = random.choice(colour)

if colour_p_2 == "Red":
    number_p_2 = random.choice(number_R_2)
    number_R_2.remove(number_p_2)

elif colour_p_2 == "Yellow":
    number_p_2 = random.choice(number_Y_2)
    number_Y_2.remove(number_p_2)

elif colour_p_2 == "Black":
    number_p_2 = random.choice(number_B_2)
    number_B_2.remove(number_p_2)

input("{0} press enter to pick up a card".format(user_1))
print("{0}'s draw : ".format(user_1))
player_1_colour = print(colour_p_1)
player_1_number = print(number_p_1)

input("{0} press enter to pick up a card".format(user_2))
print("{0}'s draw : ".format(user_2))
player_2_colour = print(colour_p_2)
player_2_number = print(number_p_2)

我认为您可以先进行随机抽样,然后在循环中使用打乱的样本,如下所示:

import random

if __name__ == '__main__':
    Play_game = True
    # create the colour and number for player 1
    number_R_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    number_Y_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    number_B_1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

    n_R = len(number_R_1)
    n_Y = len(number_Y_1)
    n_B = len(number_B_1)
    sample_R_1 = random.sample(number_R_1, n_R)
    sample_Y_1 = random.sample(number_Y_1, n_Y)
    sample_B_1 = random.sample(number_B_1, n_B)
    colour = ["Red", "Yellow", "Black"]
    R_id = 0
    Y_id = 0
    B_id = 0
    # play the game
    while Play_game == True:
        colour_p_1 = random.choice(colour)
        
        if colour_p_1 == "Red":
            number_p_1 = sample_R_1[R_id]
            R_id += 1
        elif colour_p_1 == "Yellow":
            number_p_1 = sample_Y_1[Y_id]
            Y_id += 1
        elif colour_p_1 == "Black":
            number_p_1 = sample_B_1[B_id]
            B_id += 1
#         input("press enter to pick up a card")
        player_1_colour = colour_p_1
        player_1_number = number_p_1
        print(f"Player1: {player_1_colour}, {player_1_number}")
        
        if R_id == n_R:
            colour.remove("Red")
            R_id += 1
        if Y_id == n_Y:
            colour.remove("Yellow")
            Y_id = 1
        if B_id == n_B:
            colour.remove("Black")
            B_id += 1
        if 0 == len(colour):
            break

结果:

Player1: Red, 4
Player1: Red, 5
Player1: Black, 4
Player1: Red, 8
Player1: Red, 2
Player1: Black, 8
Player1: Yellow, 8
Player1: Black, 1
Player1: Black, 9
Player1: Yellow, 2
Player1: Red, 10
Player1: Black, 10
Player1: Black, 5
Player1: Black, 7
Player1: Black, 2
Player1: Red, 6
Player1: Red, 1
Player1: Yellow, 5
Player1: Black, 3
Player1: Black, 6
Player1: Red, 7
Player1: Red, 9
Player1: Yellow, 9
Player1: Red, 3
Player1: Yellow, 4
Player1: Yellow, 10
Player1: Yellow, 7
Player1: Yellow, 3
Player1: Yellow, 6
Player1: Yellow, 1

我注释掉了input function 以显示完整运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM