How Would I Go About Making My Python Scoring System Work?
我一直在通过一个在线课程学习,我试图想出一些我可以创造的东西来"测试"我自己,所以我想出了一个石头剪刀游戏。它工作得很好,所以我决定尝试添加一种方法来跟踪你的分数与电脑的对比。不是很顺利。
以下是我的资料:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 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(' 1) Rock 2) Paper 3) Scissors') user_guess = input("Select An Option:") if(user_guess == '1'): print(' You Selected Rock') elif(user_guess == '2'): print(' You Selected Paper') elif(user_guess == '3'): print(' You 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(" Keyboard Interrupt - Exiting...") exit() #1 = Rock, 2 = Paper, 3 = Scissors def main(): while(1): print(" 1) New Game 2) View Score 3) Exit") try: option = input("Select An Option:") if option == '1': newgame() if option == '2': print(" Scores") print("Your Score:" + str(user_score)) print("AI Score:" + str(ai_score)) elif option == '3': print(' Exiting...') break else: print('%s is not an option' % option) except KeyboardInterrupt: print(" Keyboard Interrupt - Exiting...") exit() main() |
我在某个地方读到,全局变量可以工作,但通常不受欢迎。不知道为什么,但我不能说他们是0,所以不能让它起作用。在newgame()中输入ai_得分和user_得分不起作用,因为它每次运行时都将其设置为0。任何帮助都将不胜感激。
作为一个快速的补充说明,第二个
1 2 | else: print('%s is not an option' % option) |
号
在main()中,似乎总是执行并且总是说"1不是一个选项",我不知道它为什么这样做。我会假设while循环与之有关,但是我需要那些循环来保持它的运行,所以解释为什么以及如何修复将是很好的。一天结束的时候,我只是来这里了解更多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 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(' 1) Rock 2) Paper 3) Scissors') user_guess = input("Select An Option:") if(user_guess == '1'): print(' You Selected Rock') elif(user_guess == '2'): print(' You Selected Paper') elif(user_guess == '3'): print(' You 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(" Keyboard Interrupt - Exiting...") exit() #1 = Rock, 2 = Paper, 3 = Scissors def main(): game_object = newgame() while(1): print(" 1) New Game 2) View Score 3) Exit") try: option = input("Select An Option:") if option == '1': game_object.playgame() elif option == '2': print(" Scores") print("Your Score:" + str(game_object.user_score)) print("AI Score:" + str(game_object.ai_score)) elif option == '3': print(' Exiting...') break else: print('%s is not an option' % option) except KeyboardInterrupt: print(" Keyboard Interrupt - Exiting...") exit() main() |
号
上课很精彩。
编辑:
回答你关于
变量
1 | print (option, type (option)) |
在设置变量选项的行之后。
这将告诉您变量选项的类型,在这种情况下,它是一个整数。因此,首先,您需要用与整数的比较来替换与字符串的比较(如
至于第二个问题:函数内部声明或分配的变量只存在于该函数的范围内。如果需要在具有外部作用域的函数内部使用变量,则应在函数内部将其重新声明为
至少有一个问题是:你的主调有if…if…elif…else。第二个if可能需要是elif。提示:当有控制流问题时,将print语句放入每个控制分支,打印出控制变量以及其他可能相关的内容。这告诉你哪个分支被采用——在本例中,哪个分支,复数。
你不知道保持分数到底有什么问题,但我认为这是一个例外,在赋值之前引用的变量行。如果是这样的话,你应该把"全球人工智能评分"放在功能的顶部。现在的情况是,Python可以,但不喜欢,识别函数内部使用的函数外部的变量。你得用力一点。考虑:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> 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 |
顺便说一下,对于新手来说,你的代码一点也不坏。我见过很多,更糟的是!就其价值而言,我不认为全局变量对这样一个小的、被丢弃的程序是有害的。一旦你有了两个程序员,或者两个线程,或者两个月的程序工作时间间隔,Globals肯定会引起问题。