Hand Cricket游戏代码报错:NameError: 'runs'变量未定义
Hand Cricket游戏
NameError错误修复 问题描述
运行Hand Cricket游戏时触发错误:
runs1= runs NameError: name 'runs' is not defined. Did you mean: 'round'?
核心问题是变量作用域限制:第一局函数(batinning1/bowlinning1)内部定义的runs是局部变量,第二局函数(batinning2/bowlinning2)无法访问;同时第二局函数还存在得分更新、循环条件颠倒等逻辑错误。
修复步骤
- 全局变量存储第一局得分:在函数外部定义
first_innings_runs全局变量,用于保存第一局的目标得分 - 修改第一局函数:使用
global关键字声明全局变量,将第一局计算出的得分赋值给它 - 修正第二局逻辑:
- 将错误的
runs+= bat1改为runs2 += bat1,确保第二局得分正确累加 - 调整循环条件,让游戏在达到目标得分或出局时及时终止
- 修正胜负判断逻辑,避免条件颠倒
- 将错误的
- 优化游戏体验:添加实时分数显示,让玩家清晰知晓当前进度
修复后的完整代码
import random import time # 全局变量存储第一局得分 first_innings_runs = 0 print("Welcome to Hand Cricket!") toss= input("Heads or Tails?") cointoss = ["heads", "tails"] inning = ["bat", "ball"] coin= random.choice(cointoss) Toss= toss.lower() if Toss.strip() not in cointoss: while Toss.strip() not in cointoss: print("Input a valid choice") toss= input('Heads or Tails?') Toss= toss.lower() if Toss.lower()== coin.lower(): choice= input('You have won the toss! Do you choose to bat or ball') Choice= choice.lower() while Choice.strip() not in inning: print("Input a valid choice") choice= input('Do you choose to bat or bowl') Choice= choice.lower() else: print("You have lost the toss :( Computer is choosing") time.sleep(2) choice= random.choice(inning) Choice= choice.lower() print("Well then, computer has chosen to",Choice,"first!") def batinning1(): global first_innings_runs print("First Innings Starts now! Start Batting!") runs= 0 for i in range(1,121): bat1= int(input("Enter number of runs you want to take")) if bat1 not in range(0,7): while bat1 not in range(0,7): print("What is that shot? Enter between 0 and 6") bat1= int(input("Enter number of runs you want to take")) bowl1= random.randint(0, 6) time.sleep(2) if bat1==bowl1: print("HOWZAAT!!! You are out!") print("Computer chose", bowl1) break else: runs+= bat1 if bat1==0: print("Batsman Defended that one quite well!") elif bat1==1: print("Batsman takes a quick single") elif bat1==2: print("Batsman tries to hit the ball, and takes quick double") elif bat1==3: print("Batsman has taken a rare triple") elif bat1==4: print("ITS A FOUUURRRRR!!! Batsman has hit that one for a boundary!") elif bat1==5: print("An overthrow by the fielders, and its a boundary, meaning 5 runs!") elif bat1==6: print("ITS A SIX! WHAT A SHOT BY THE BATSMAN!") first_innings_runs = runs print("You have scored", runs ,"runs! Now you have to defend", runs+1,"runs") def bowlinning1(): global first_innings_runs print("First Innings Starts now! Start Bowling!") runs= 0 for i in range(1,121): bowl1= int(input("What do you think the computer will score?")) if bowl1 not in range(0,7): while bowl1 not in range(0,7): print("What is that shot? Enter between 0 and 6") bowl1 = int(input("What do you think the computer will score?")) bat1= random.randint(0, 6) time.sleep(2) if bat1==bowl1: print("HOWZAAT!!! ITS A WICKET!") break else: runs+= bat1 if bat1==0: print("Batsman Defended that one quite well!") elif bat1==1: print("Batsman takes a quick single") elif bat1==2: print("Batsman tries to hit the ball, and takes quick double") elif bat1==3: print("Batsman has taken a rare triple") elif bat1==4: print("ITS A FOUUURRRRR!!! Batsman has hit that one for a boundary!") elif bat1==5: print("An overthrow by the fielders, and its a boundary, meaning 5 runs!") elif bat1==6: print("ITS A SIX! WHAT A SHOT BY THE BATSMAN!") first_innings_runs = runs print("Computer has scored", runs,"runs! You now need to score", runs+1,"runs to win!") def batinning2(): print("Second Innings Starts now! Start Batting!") runs2= 0 target = first_innings_runs + 1 for i in range(1,121): if runs2 >= target: break bat1= int(input("Enter number of runs you want to take")) if bat1 not in range(0,7): while bat1 not in range(0,7): print("What is that shot? Enter between 0 and 6") bat1= int(input("Enter number of runs you want to take")) bowl1= random.randint(0, 6) time.sleep(2) if bat1==bowl1: print("HOWZAAT!!! You are out! You lose this match :(") print("Computer chose", bowl1) break else: runs2 += bat1 if bat1==0: print("Batsman Defended that one quite well!") elif bat1==1: print("Batsman takes a quick single") elif bat1==2: print("Batsman tries to hit the ball, and takes quick double") elif bat1==3: print("Batsman has taken a rare triple") elif bat1==4: print("ITS A FOUUURRRRR!!! Batsman has hit that one for a boundary!") elif bat1==5: print("An overthrow by the fielders, and its a boundary, meaning 5 runs!") elif bat1==6: print("ITS A SIX! WHAT A SHOT BY THE BATSMAN!") print(f"Current score: {runs2}/{target}") if runs2 >= target: print("AND THE BATSMAN FINISHES IN STYLE! ITS A WIN FOR YOU!!!") def bowlinning2(): print("Second Innings Starts now! Start Bowling!") runs2= 0 target = first_innings_runs + 1 for i in range(1,121): if runs2 >= target: print(f"Computer scored {runs2} runs, reaching the target of {target}! You lose this match :(") break bowl1= int(input("What do you think the computer will score?")) if bowl1 not in range(0,7): while bowl1 not in range(0,7): print("What is that shot? Enter between 0 and 6") bowl1 = int(input("What do you think the computer will score?")) bat1= random.randint(0, 6) time.sleep(2) if bat1==bowl1: print(f"HOWZAAT!!! ITS A WICKET! YOU WIN THIS MATCH BY {first_innings_runs - runs2} runs!") break else: runs2 += bat1 if bat1==0: print("Batsman Defended that one quite well!") elif bat1==1: print("Batsman takes a quick single") elif bat1==2: print("Batsman tries to hit the ball, and takes quick double") elif bat1==3: print("Batsman has taken a rare triple") elif bat1==4: print("ITS A FOUUURRRRR!!! Batsman has hit that one for a boundary!") elif bat1==5: print("An overthrow by the fielders, and its a boundary, meaning 5 runs!") elif bat1==6: print("ITS A SIX! WHAT A SHOT BY THE BATSMAN!") print(f"Computer's current score: {runs2}/{target}") if Toss.lower()== coin.lower(): if Choice == 'bat': batinning1() elif Choice== 'ball': bowlinning1() else: if Choice == 'bat': bowlinning1() elif Choice== 'ball': batinning1() time.sleep(2) if Toss.lower()== coin.lower(): if choice.lower() == 'bat': bowlinning2() elif choice.lower()== 'ball': batinning2() else: if choice.lower() == 'bat': batinning2() elif choice.lower()== 'ball': bowlinning2()
关键修复点说明
- 全局变量
first_innings_runs:打破函数作用域限制,实现第一局得分向第二局的传递 - 第二局得分修正:将错误的
runs累加改为runs2累加,确保第二局得分独立计算 - 游戏流程优化:添加实时分数显示,调整循环终止条件,让游戏逻辑更严谨
内容的提问来源于stack exchange,提问作者SunnyandBunny
相关产品推荐
相关产品推荐

