【发布时间】:2021-09-21 21:49:50
【问题描述】:
我正在尝试编写与水蛇枪游戏(石头、纸、剪刀)相关的python代码。
如果我使用此代码:
import random
usscore = 0
pcscore = 0
wsg = ["Water", "Snake", "Gun"]
inp = [1,2,3]
wsginputT = 0
wsginpw = ""
def wsgf(wsginpw,compran,pcscore,wsginput,usscore):
if wsginpw == compran:
print("AARGH its a draw")
pass
else:
if wsginput == 1 and compran == wsg[1]:#WATER VS SNAKE
print("LOLLL")
print("HAHA Snake drunk all the water")
pcscore = pcscore +1
else:
if wsginput == 1 and compran == wsg[2]:#WATER VS GUN
print("Hmmmm will see ya next time! ")
print("Gun had no chance against WATER")
usscore = usscore + 1
else:
if wsginput == 2 and compran == wsg[0]:#SNAKE VS WATER
print("Hmmmm will see ya next time! ")
print("HUH Your snake drunk all the water")
usscore = usscore + 1
else:
if wsginput == 2 and compran == wsg[2]:#SNAKE VS GUN
print("LOLLL")
print("HAHA My Gun killed your snake")
pcscore = pcscore + 1
else:
if wsginput == 3 and compran == wsg[0]:#GUN VS WATER
print("LOLLL ")
print("HAHA your gun is drowned in water!")
pcscore = pcscore + 1
else:
if wsginput == 3 and compran == wsg[1]:#GUN VS SNAKE
print("Hmmmm will see ya next time!")
print("Not Fair my snake was killed by your gun")
usscore = usscore + 1
print("Pc score",pcscore, "User Score", usscore)
for ten in range(10):
while True:
try:
wsginput = int(input("""Please enter
1 FOR WATER
2 FOR SNAKE
3 FOR GUN\n"""))
if wsginput in inp:
break
else:
print("Please enter value between 1 and 3 inclusive")
continue
except:
print("Please enter a valid integer")
continue
wsginpw = wsg[wsginput-1]
# print(wsginpw)
compran = random.choice(wsg)
# print(compran)
wsgf(wsginpw,compran,pcscore,wsginput,usscore)
print("Your score is ", usscore)
print("Computer's score is ", pcscore)
if usscore > pcscore:
print("You Won")
else:
print("You Lose")
我最后得到的分数是0。你可以运行它。
通过引入global函数解决的问题,代码运行流畅,但我记得有人说不要使用全局有问题Headbutting UnboundLocalError (blah…blah…blah)
globals 的代码如下:
import random
usscore = 0
pcscore = 0
wsg = ["Water", "Snake", "Gun"]
inp = [1,2,3]
wsginputT = 0
wsginpw = ""
def wsgf():
global wsginpw
global compran
global pcscore
global wsginput
global usscore
if wsginpw == compran:
print("AARGH its a draw")
pass
else:
if wsginput == 1 and compran == wsg[1]:#WATER VS SNAKE
print("LOLLL")
print("HAHA Snake drunk all the water")
pcscore = pcscore +1
else:
if wsginput == 1 and compran == wsg[2]:#WATER VS GUN
print("Hmmmm will see ya next time! ")
print("Gun had no chance against WATER")
usscore = usscore + 1
else:
if wsginput == 2 and compran == wsg[0]:#SNAKE VS WATER
print("Hmmmm will see ya next time! ")
print("HUH Your snake drunk all the water")
usscore = usscore + 1
else:
if wsginput == 2 and compran == wsg[2]:#SNAKE VS GUN
print("LOLLL")
print("HAHA My Gun killed your snake")
pcscore = pcscore + 1
else:
if wsginput == 3 and compran == wsg[0]:#GUN VS WATER
print("LOLLL ")
print("HAHA your gun is drowned in water!")
pcscore = pcscore + 1
else:
if wsginput == 3 and compran == wsg[1]:#GUN VS SNAKE
print("Hmmmm will see ya next time!")
print("Not Fair my snake was killed by your gun")
usscore = usscore + 1
print("Pc score",pcscore, "User Score", usscore)
for ten in range(10):
while True:
try:
wsginput = int(input("""Please enter
1 FOR WATER
2 FOR SNAKE
3 FOR GUN\n"""))
if wsginput in inp:
break
else:
print("Please enter value between 1 and 3 inclusive")
continue
except:
print("Please enter a valid integer")
continue
wsginpw = wsg[wsginput-1]
# print(wsginpw)
compran = random.choice(wsg)
# print(compran)
wsgf()
print("Your score is ", usscore)
print("Computer's score is ", pcscore)
if usscore > pcscore:
print("You Won")
else:
print("You Lose")
请有人帮我找到不使用globals 的方法,请记住我是初学者,正在学习。如果您使用困难的东西,请解释它。
另外,关于如何缩短我的代码的任何提示?
【问题讨论】:
-
使用
elif而不是在else中嵌套if。 -
谢谢,但是全球呢?
-
没什么,我还没读完你的代码。由于极度嵌套,很难理解。
-
对不起
-
如果您有工作代码,我建议您使用Code Review Stack Exchange 对其进行批评并获得改进建议。
标签: python global-variables globals