Home > other >  How to use the function to give different Python variable assignment
How to use the function to give different Python variable assignment

Time:01-21

Python is a small white one, write a small game, in the if and the print is one part of the
If the zb==1:
Ywuqi="l Long Ji"
YwuqiN=300
Print (' you purchased % s \ n % ywuqi)
If the zb==2:
Ywuqi="penetration stone axe"
YwuqiN=700
Print (' you purchased % s \ n % ywuqi)
If the zb==3:
Ywuqi="flower of knife"
YwuqiN=500
Print (' you purchased % s \ n % ywuqi)
If the zb==4:
Ywuqi="vampire sword"
YwuqiN=300
Print (' you purchased % s \ n % ywuqi)
If the zb==5:
Yfangju="wuzhuang"
YfangjuN=400
Print (' you purchased % s \ n % yfangju)
If the zb==6:
Yfangju="; "
YfangjuN=200
Print (' you purchased % s \ n % yfangju)
Because are duplicated code, recently learned function again, so I want to use function instead of a moment, I think such a code (only in one situation, for example)
Yfangju="no"
YfangjuN=0
Jianaijiu={yfangju: yfangjuN}
Def maizb (lei, Ming, nai) :
Global yfangjuN
Global yfangju
Lei Ming=
Lei jianaijiu []=nai
Print (' you purchased % s \ n % Ming)
Print (yfangju yfangjuN)
Maizb yfangju, 'wuzhuang, (400)
But it is directly to lei assignment to change yfangju and yfangjuN two variables do not move
Can I hope is like in the picture with this function
Please bosses give directions

CodePudding user response:

This is not feasible?
 d={1: [' rise Long Ji, 300], 
2: [' penetration stone axe, 700],
3: [' of flower knife, 500],
4: [' vampire sword, 300],
5: [' wuzhuang, 400],
6: ['; ', 200],
7: [' soft hedgehog armour, 300],
}
Zb=random. Randint (1, 13)
If the zb & lt; 6:
Ywuqi [zb] [0]=d
YwuqiN=d (zb) [1]
The else:
Yfangju [zb] [0]=d
YfangjuN=d (zb) [1]

CodePudding user response:

Use a dictionary, but no function
  • Related