Dies ist für ein textbasiertes Kartenspiel.Setzen Sie eine Variable auf ein zufälliges Element von einem Array und nicht auf den Wert des Elements. --- python ---
Ich habe versucht, Python zu bekommen, um zufällig eine Variable aus einem Array auszuwählen und den Namen der Variablen in Fa
zu speichern. Es speichert jedoch nur den Wert der Variablen Fa
und nicht den Namen der Variablen.
Mein Code:
import random
k='King'
q='Queen'
j='Jack'
t='Ten'
n='Nine'
e='Eight'
s='Seven'
i='Six'
f='Five'
u='Four'
h='Three'
w='two'
a='ace'
c='of clubs'
d='of diamonds'
p='of spades'
r='of hearts'
#clubs
A1=k+' '+c
B1=q+' '+c
C1=j+' '+c
D1=t+' '+c
E1=n+' '+c
F1=e+' '+c
G1=s+' '+c
H1=i+' '+c
I1=f+' '+c
J1=u+' '+c
K1=h+' '+c
L1=w+' '+c
M1=a+' '+c
#diamonds
A2=k+' '+d
B2=q+' '+d
C2=j+' '+d
D2=t+' '+d
E2=n+' '+d
F2=e+' '+d
G2=s+' '+d
H2=i+' '+d
I2=f+' '+d
J2=u+' '+d
K2=h+' '+d
L2=w+' '+d
M2=a+' '+d
#spades
A3=k+' '+p
B3=q+' '+p
C3=j+' '+p
D3=t+' '+p
E3=n+' '+p
F3=e+' '+p
G3=s+' '+p
H3=i+' '+p
I3=f+' '+p
J3=u+' '+p
K3=h+' '+p
L3=w+' '+p
M3=a+' '+p
#hearts
A=k+' '+r
B=q+' '+r
C=j+' '+r
D=t+' '+r
E=n+' '+r
F=e+' '+r
G=s+' '+r
H=i+' '+r
I=f+' '+r
J=u+' '+r
K=h+' '+r
L=w+' '+r
M=a+' '+r
clubs=[A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1,L1,M1]
diamonds=[A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2,L2,M2]
hearts=[A3,B3,C3,D3,E3,F3,G3,H3,I3,J3,K3,L3,M3]
spades=[A,B,C,D,E,F,G,H,I,J,K,L,M]
CardTotal=len(clubs)
X=''
Da=''
Fa=''
sep='\n And \n'
hand=''
Cardhand=7
while(Cardhand != 0):
Cardhand=Cardhand-1
Da=random.choice(['1', '2', '3','4'])
if(Da=='1'):
Fa=random.choice(clubs)
elif(Da=='2'):
Fa=random.choice(hearts)
elif(Da=='3'):
Fa=random.choice(spades)
elif(Da=='4'):
Fa=random.choice(diamonds)
hand=hand+sep+Fa+sep
if(Fa in clubs):
clubs.pop(Fa)
elif(Fa in diamonds):
diamonds.pop(Fa)
elif(Fa in hearts):
hearts.pop(Fa)
elif(Fa in spades):
spades.pop(Fa)
Da=''
Fa=''
cards=[clubs,diamonds,hearts,spades]
nur ersetzen z.B. 'random.choice (clubs)' durch 'i = random.choice (Bereich (len (clubs))'. Dann 'i' ist der Index und' clubs [i] 'ist der Wert. –