[英]How Would I Go About Making My Python Scoring System Work?
我一直在学习在线课程,我试图想出一些我可以创造的东西来“测试”我自己,因为我想出了一个石头剪刀游戏。 它运作良好所以我决定尝试添加一种跟踪你的分数与计算机的方法。 没那么顺利。
这就是我所拥有的:
from random import randint
ai_score = 0
user_score = 0
def newgame():
print('New Game')
try:
while(1):
ai_guess = str(randint(1,3))
print('\n1) Rock \n2) Paper \n3) Scissors')
user_guess = input("Select An Option: ")
if(user_guess == '1'):
print('\nYou Selected Rock')
elif(user_guess == '2'):
print('\nYou Selected Paper')
elif(user_guess == '3'):
print('\nYou Selected Scissors')
else:
print('%s is not an option' % user_guess)
if(user_guess == ai_guess):
print('Draw - Please Try Again')
elif (user_guess == '1' and ai_guess == '2'):
print("AI Selected Paper")
print("Paper Beats Rock")
print("AI Wins!")
ai_score += 1
break
elif (user_guess == '1' and ai_guess == '3'):
print("AI Selected Scissors")
print("Rock Beats Scissors")
print("You Win!")
user_score += 1
break
elif (user_guess == '2' and ai_guess == '1'):
print("AI Selected Rock")
print("Paper Beats Rock")
print("You Win!")
user_score += 1
break
elif (user_guess == '2' and ai_guess == '3'):
print("AI Selected Scissors")
print("Scissors Beats Paper")
print("AI Wins!")
ai_score += 1
break
elif (user_guess == '3' and ai_guess == '1'):
print("AI Selected Rock")
print("Rock Beats Scissors")
print("AI Wins!")
ai_score += 1
break
elif (user_guess == '3' and ai_guess == '2'):
print("AI Selected Paper")
print("Scissors Beats Paper")
print("You Win!")
user_score += 1
break
else:
pass
break
except KeyboardInterrupt:
print("\nKeyboard Interrupt - Exiting...")
exit()
#1 = Rock, 2 = Paper, 3 = Scissors
def main():
while(1):
print("\n1) New Game \n2) View Score \n3) Exit")
try:
option = input("Select An Option: ")
if option == '1':
newgame()
if option == '2':
print("\nScores")
print("Your Score: " + str(user_score))
print("AI Score: " + str(ai_score))
elif option == '3':
print('\nExiting...')
break
else:
print('%s is not an option' % option)
except KeyboardInterrupt:
print("\nKeyboard Interrupt - Exiting...")
exit()
main()
我在某处读到全局变量可以起作用但通常不赞成。 不知道为什么然后我不能说他们'= 0因此无法让它工作。 将ai_score和user_score放在newgame()中不起作用,因为每次重新运行时它都会将其设置为0。 任何帮助将非常感激。
作为一个快速的额外说明,第二个
else:
print('%s is not an option' % option)
在main()似乎总是执行并总是说“1不是一个选项”,我不知道它为什么这样做。 我会假设与while循环有关,但我需要它们来保持运行,所以解释为什么以及如何修复将是伟大的。 在一天结束时,我只是在这里了解更多。
from random import randint
class newgame():
ai_score = 0
user_score = 0
def __init__(self):
self.ai_score = 0
self.user_score = 0
def playgame(self):
print('New Game')
try:
while(1):
ai_guess = str(randint(1,3))
print('\n1) Rock \n2) Paper \n3) Scissors')
user_guess = input("Select An Option: ")
if(user_guess == '1'):
print('\nYou Selected Rock')
elif(user_guess == '2'):
print('\nYou Selected Paper')
elif(user_guess == '3'):
print('\nYou Selected Scissors')
else:
print('%s is not an option' % user_guess)
if(user_guess == ai_guess):
print('Draw - Please Try Again')
elif (user_guess == '1' and ai_guess == '2'):
print("AI Selected Paper")
print("Paper Beats Rock")
print("AI Wins!")
self.ai_score += 1
break
elif (user_guess == '1' and ai_guess == '3'):
print("AI Selected Scissors")
print("Rock Beats Scissors")
print("You Win!")
self.user_score += 1
break
elif (user_guess == '2' and ai_guess == '1'):
print("AI Selected Rock")
print("Paper Beats Rock")
print("You Win!")
self.user_score += 1
break
elif (user_guess == '2' and ai_guess == '3'):
print("AI Selected Scissors")
print("Scissors Beats Paper")
print("AI Wins!")
self.ai_score += 1
break
elif (user_guess == '3' and ai_guess == '1'):
print("AI Selected Rock")
print("Rock Beats Scissors")
print("AI Wins!")
self.ai_score += 1
break
elif (user_guess == '3' and ai_guess == '2'):
print("AI Selected Paper")
print("Scissors Beats Paper")
print("You Win!")
self.user_score += 1
break
else:
pass
break
except KeyboardInterrupt:
print("\nKeyboard Interrupt - Exiting...")
exit()
#1 = Rock, 2 = Paper, 3 = Scissors
def main():
game_object = newgame()
while(1):
print("\n1) New Game \n2) View Score \n3) Exit")
try:
option = input("Select An Option: ")
if option == '1':
game_object.playgame()
elif option == '2':
print("\nScores")
print("Your Score: " + str(game_object.user_score))
print("AI Score: " + str(game_object.ai_score))
elif option == '3':
print('\nExiting...')
break
else:
print('%s is not an option' % option)
except KeyboardInterrupt:
print("\nKeyboard Interrupt - Exiting...")
exit()
main()
课程很精彩。 __init__
是此类的构造函数。 它基本上使对象立即为类,并将变量设置为您想要的。 game_object = newgame()
生成类对象并将其存储到game_object中。 要获取game_object的类变量,我们使用game_object.ai_score
。 由于您创建了一个类对象,因此它的类变量仍然在您创建的对象的范围内,即使它可能在您的函数之外。 通常,如果我需要在函数之外使用变量,并且很想使用Global,我会创建一个类。 在某些情况下你不会想要这个,但我个人并没有遇到过这个。 此外,您可能希望查看评论中有关使用字典作为选项的内容。 还有其他问题吗?
编辑:
要回答关于print('%s is not an option' % option)
的新问题print('%s is not an option' % option)
总是在运行,因为在你的代码中你有if option == '1':
然后if option == '2':
你想要选项2是elif。 我在我的代码中修复了它。 如果语句是块。 因为你开始了一个新的if,否则先没先检查是否要查看它是否是一个有效的选项。 从某种意义上说,它超出了它的范围。 所以既然你的代码基本上是说选项等于1? 它等于2或3或其他什么? 请注意这些是两个不同的问题?
似乎变量option
不是'1',对吧? 好吧,那是因为函数input
不返回字符串,而是返回integer
。 你可以通过在这个程序中添加一点跟踪来看到这一点。
print (option, type (option))
在设置了变量选项的行之后。
这将告诉您变量选项的类型,在这种情况下它是一个整数。 首先,你需要替换字符串的比较(比如if option == '1':
通过与整数的比较,即: if option == 1:
.
至于第二个问题:在函数内声明或赋值的变量仅存在于该函数的范围内。 如果你需要在具有外部作用域的函数内使用变量,那么它应该在函数内部重新声明为global
(即使它们“不受欢迎” - 并且有很好的理由)。 在def newgame():
的开头def newgame():
你需要再次声明你的全局变量: global ai_score, user_score
。 您还可以使用类来熟悉面向对象的编程并编写更好的代码。 此程序还有其他错误,但我相信你会发现。
至少有一个问题是:你的主要是......如果......如果... elif ...其他。 第二个可能需要是一个elif。 提示:当您遇到控制流问题时,将print语句放在每个控制分支中,打印出控制变量以及可能相关的所有其他内容。 这告诉你正在采取哪个分支 - 在这种情况下,哪个分支,复数。
你没有确切地说出保持分数的问题是什么,但我想这是在赋值之前引用的变量行的异常。 如果是这样,你应该把“global ai_score”放在函数的顶部。 发生了什么事情,Python可以但不喜欢识别函数内部正在使用的函数之外的变量。 你必须推动一下。 考虑:
>>> bleem = 0
>>> def incrbleem():
... bleem += 1
...
>>> incrbleem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in incrbleem
UnboundLocalError: local variable 'bleem' referenced before assignment
>>> def incrbleem():
... global bleem
... bleem += 1
...
>>> bleem
0
>>> incrbleem()
>>> bleem
1
顺便说一下,对于新手来说,你的代码一点都不差。 我见过很多,甚至更糟! 对于它的价值,我不认为全局变量对于像这样的小型丢弃程序是不利的。 一旦你有两个程序员,或两个线程,或两个月之间的程序工作会话,全局变量肯定会导致问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.