Python新手,找不到bug

时间:2016-06-03 04:23:58

标签: python debugging

我是Python的新手(第3天),我只是想创建一个基本的Rock,Paper,Scissors。我看到一个我无法在代码中找到的错误,并希望有人可以提供帮助。以下是以下代码输出:

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?

输入“否”进行复赛后,正在打印“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()

另外,如果您有关于如何清理代码的任何建议,那将不胜感激!

由于

3 个答案:

答案 0 :(得分:2)

查看您的功能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.")

这应该照顾rematch。您也可以应用此方法来消除其他变量。

答案 1 :(得分:0)

您的错误主要是一个小的逻辑错误。虽然具有真实条件的循环通常是不好的做法。关键是你可以像这样重写rematchinvalid(),并且在逻辑上仍然等同于你想要实现的目标。

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

此外,尝试将其实现为其他功能。我注意到的另一件事 -

Rock, Paper, or Scissors?paper
a  threw  paper
b  threw  paper
Draw! Get Ready!.
3
2
1
GO!
Rock, Paper or Scissors?paper
a  threw  paper
b  threw  rock
a  win.
Rematch?no
Goodbye.
a  win.
Rematch?no
Goodbye.
Goodbye.
a  win.
Rematch?no
Goodbye.
Goodbye.
a  win.
Rematch?no
Goodbye. 

如果你得到平局,你应该设置一个标志来退出该功能。否则你最终会在输出中得到这样的东西!

答案 2 :(得分:0)

  

如果您有关于如何清理代码的任何建议,那将不胜感激

codes = {'rock':0, 'paper':1, 'scissors':2}
...
def winner():
    outcome = (codes[P1]-codes[P2])%3
    if outcome == 0:
        # it's a draw
    elif outcome == 1:
        # player 1 wins
    else:
        # player2 wins