gpt4 book ai didi

python - Python新手,找不到bug

转载 作者:行者123 更新时间:2023-12-01 03:55:11 24 4
gpt4 key购买 nike

我是 Python 新手(第三天),我只是想创建一个基本的石头、剪刀、布。我发现代码中找不到一个错误,希望有人能提供帮助。这是下面的输出,代码如下:

Welcome to Rock, Paper, Scissors!
Player 1 name?b
Player 2 name?s
3
2
1
GO!
Rock, Paper or Scissors?rock
b threw rock
s threw rock
Draw! Get Ready!.
3
2
1
GO!
Rock, Paper or Scissors?rock
b threw rock
s threw scissors
b win.
Rematch?no
Goodbye.
b win.
Rematch?

重赛输入“no”后,打印“b win”。还问“复赛?”再次。下面是代码:

import time
import random
picks=["rock","paper","scissors"]
answers=["yes","no"]
rock="rock"
paper="paper"
scissors="scissors"
yes="yes"
no="no"
invalid=""
#############################################Defining Functions#########################################################
def rename1():
global name1
while True:
if name1 is invalid:
name1 = input("Player 1 name?")
if name1 is not invalid:
break

def rename2():
global name2
while True:
if name2 is invalid:
name2 = input("Player 2 name?")
if name2 is not invalid:
break

def rematchinvalid():
global rematch
while True:
if rematch not in answers:
print("Invalid, try again..")
rematch = input("Rematch?")
if rematch in answers:
break

def Rethrow1():
global P1
while True:
if P1 not in picks:
print("Invalid, try again..")
P1 = input("Rock, Paper, or Scissors?")
if P1 in picks:
break

def start():
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print("GO!")

def RPS():
global P1
global P2
global rematch
P1 = input("Rock, Paper or Scissors?")
P2 = random.choice(picks)
if P1 not in picks:
Rethrow1()
battle()
winner()

def battle():
print(name1," threw ",P1)
print(name2," threw ",P2)

def winner():
global rematch
if P1 == P2:
print("Draw! Get Ready!.")
start()
RPS()
if P1 == rock and P2 == scissors:
print(name1," win.")
if P1 == rock and P2 == paper:
print(name2," win.")
if P1 == scissors and P2 == rock:
print(name2," win.")
if P1 == scissors and P2 == paper:
print(name1," win.")
if P1 == paper and P2 == rock:
print(name1," win.")
if P1 == paper and P2 == scissors:
print(name2," win.")
rematch = input("Rematch?")
if rematch not in answers:
rematchinvalid()
replay()

def replay():
if rematch == yes:
start()
RPS()
if rematch == no:
print("Goodbye.")
################################################Game Start##############################################################
print("Welcome to Rock, Paper, Scissors!")
name1 = input("Player 1 name?")
if name1 is invalid:
rename1()
name2 = input("Player 2 name?")
if name2 is invalid:
rename2()
start()
RPS()

此外,如果您对如何清理代码有任何建议,我们将不胜感激!

谢谢

最佳答案

看看你的函数winner:

def winner():
global rematch
if P1 == P2:
print("Draw! Get Ready!.")
start()
RPS()
if P1 == rock and P2 == scissors:
print(name1," win.")
if P1 == rock and P2 == paper:
print(name2," win.")
if P1 == scissors and P2 == rock:
print(name2," win.")
if P1 == scissors and P2 == paper:
print(name1," win.")
if P1 == paper and P2 == rock:
print(name1," win.")
if P1 == paper and P2 == scissors:
print(name2," win.")
rematch = input("Rematch?")
if rematch not in answers:
rematchinvalid()
replay()

当出现平局时,您打印Draw!准备好!,调用start(),调用RPS(),然后呢?然后,您没有退出该函数,而是让控制直接进入下面的代码,该代码再次显示获胜者名称,然后在退出该函数之前再次要求重新匹配。我把它留给你来修复它。

建议:请不要使用全局变量。

更新

这里有一些消除全局变量的建议:将信息传递给函数并从函数返回信息。例如,这里有一种消除全局变量rematch的方法。该变量首先在 winner() 中使用,然后传递给 replay()。另外,rematchinvalid()将用户的输入获取到该变量中,并将其传回winner,因此重新匹配的信息流为:

rematchinvalid <==> winner ==> replay

考虑到这一点,我们可以这样修复rematchinvalid():

def rematchinvalid(rematch):
# Remove the global statement here
while True:
if rematch not in answers:
print("Invalid, try again..")
rematch = input("Rematch?")
if rematch in answers:
break
return rematch # Return rematch to the caller

对于winner(),我们将从rematchinvalid()接收信息并将其传递给replay():

def winner():
# Remove global statement
if P1 == P2:
print("Draw! Get Ready!.")
start()
RPS()
return # Fix for your problem
if P1 == rock and P2 == scissors:
print(name1," win.")
if P1 == rock and P2 == paper:
print(name2," win.")
if P1 == scissors and P2 == rock:
print(name2," win.")
if P1 == scissors and P2 == paper:
print(name1," win.")
if P1 == paper and P2 == rock:
print(name1," win.")
if P1 == paper and P2 == scissors:
print(name2," win.")
rematch = input("Rematch?")
if rematch not in answers:
rematch = rematchinvalid(rematch) # Get the valid rematch
replay(rematch) # Pass rematch to replay

最后,对于重播,我们可以接受rematch作为参数:

def replay(rematch):
if rematch == yes:
start()
RPS()
if rematch == no:
print("Goodbye.")

这应该可以解决重新匹配的问题。您也可以应用此方法来消除其他变量。

关于python - Python新手,找不到bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37605910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com