Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 石头、布、剪刀-当有';这是平局_Python_Python 3.x - Fatal编程技术网

Python 石头、布、剪刀-当有';这是平局

Python 石头、布、剪刀-当有';这是平局,python,python-3.x,Python,Python 3.x,我对Python的编码非常陌生,因为我是基础知识,每一个代码实际上都是一个巨大的代码块,充满了混乱的代码。我现在正在尝试做的是一款石头剪刀蜥蜴斯波克游戏,它将在以后被转化为一款视觉游戏,希望能成为我手机上的一款基础应用程序,让我的学习更敏捷,不管怎样,下面是代码 我想做的是,当玩家和电脑打成平局时,询问用户是否想再打一次,我无法正确理解,我将感谢任何帮助 import random computerChoice = random.randint(1,5) playerChoosing = Tr

我对Python的编码非常陌生,因为我是基础知识,每一个代码实际上都是一个巨大的代码块,充满了混乱的代码。我现在正在尝试做的是一款石头剪刀蜥蜴斯波克游戏,它将在以后被转化为一款视觉游戏,希望能成为我手机上的一款基础应用程序,让我的学习更敏捷,不管怎样,下面是代码

我想做的是,当玩家和电脑打成平局时,询问用户是否想再打一次,我无法正确理解,我将感谢任何帮助

import random

computerChoice = random.randint(1,5)
playerChoosing = True
answer = True

if computerChoice == 1:
    computerChoice = "rock"
elif computerChoice == 2:
    computerChoice = "paper"
elif computerChoice == 3:
    computerChoice = "scissors"
elif computerChoice == 4:
    computerChoice = "spock"
elif computerChoice == 5:
    computerChoice = "lizard"

while playerChoosing == True:
    print("rock, paper, scissors, lizard, spock, What do you choose?")
    playerChoice = input()
    if playerChoice == "rock":
        print("you chose rock")
        playerChoosing = False
    elif playerChoice == "paper":
        print("you chose paper!")
        playerChoice = "paper"
        playerChoosing = False
    elif playerChoice == "scissors":
        print("you chose scissors!")
    playerChoice = "scissors"
    playerChoosing = False
elif playerChoice == "lizard":
    print("you chose lizard!")
    playerChoice = "lizard"
    playerChoosing = False
elif playerChoice == "spock":
    print("you chose spock!")
    playerChoice = "spock"
    playerChoosing = False
else:
    print("this is not an option, please try again")
    playerChoosing = True

input()

print("The computer chose: " +computerChoice+ "!")

input()
#lose


while answer == True:
    if playerChoice == "rock" and computerChoice == "paper":
        print("you lose!, the rock was covered by the paper!")   
    elif playerChoice == "paper" and computerChoice == "scissors":
        print("you lose!, scissors cut the paper in half")    
    elif playerChoice == "scissors" and computerChoice == "rock":
        print("you lose!, rock crushed the scissors")    
    elif playerChoice == "spock" and computerChoice == "lizard":
        print("you lose!, lizard poisons spock")    
    elif playerChoice == "scissors" and computerChoice == "spock":
        print("you lose!, spock smashes scissors")    
    elif playerChoice == "rock" and computerChoice == "spock":
        print("you lose!, spock vaporizes the rock")   
    elif playerChoice == "paper" and computerChoice == "lizard":
        print("you lose!, lizard eats paper")    
    elif playerChoice == "lizard" and computerChoice == "rock":
        print("you lose!, rock crushes lizard")    
    elif playerChoice == "lizard" and computerChoice == "scissors":
        print("you lose!, scissors kills lizard")    
    elif playerChoice == "spock" and computerChoice == "paper":
        print("you lose!, paper disproves spock")
#win   
elif playerChoice == "paper" and computerChoice == "rock":
    print("you win!, the rock was covered by the paper!")    
elif playerChoice == "scissors" and computerChoice == "paper":
    print("you win!, scissors cut the paper in half!") 
elif playerChoice == "rock" and computerChoice == "scissors":
    print("you win!, rock crushed the scissors!")    
elif playerChoice == "lizard" and computerChoice == "spock":
    print("you win!, lizard poisons spock!")   
elif playerChoice == "spock" and computerChoice == "scissors":
    print("you win!, spock smashes scissors!")   
elif playerChoice == "lizard" and computerChoice == "paper":
    print("you win!, lizard eats paper!")   
elif playerChoice == "rock" and computerChoice == "lizard":
    print("you win!, rock crushes lizard!") 
elif playerChoice == "scissors" and computerChoice == "lizard":
    print("you win!, scissors kills lizard!")  
elif playerChoice == "paper" and computerChoice == "spock":
    print("you win!, paper disproves spock!")
elif playerChoice == "spock" and computerChoice == "rock":
    print("you win!, spock vaporizes rock!")
#draw
elif playerChoice == "paper" and computerChoice == "paper":
    print("It's a draw, want to try again?, please type YES or NO: ")
    answer = input()
    if answer == "yes":
        answer = False
    else:
        break
elif playerChoice == "rock" and computerChoice == "rock":
    print("It's a draw, want to try again?, please type YES or NO: ")
    answer = input()
    if answer == "yes":
        answer = False
    else:
        break
elif playerChoice == "scissors" and computerChoice == "scissors":
    print("It's a draw, want to try again?, please type YES or NO: ")
    answer = input()
    if answer == "yes":
        answer = False
    else:
        break
elif playerChoice == "lizard" and computerChoice == "lizard":
    print("It's a draw, want to try again?, please type YES or NO: ")
    answer = input()

elif playerChoice == "spock" and computerChoice == "spock":
    print("It's a draw, want to try again?, please type YES or NO: ")
    answer = input()

与其他一些人相比,我对Python还不太熟悉,但到目前为止,我学到了很多。因此,这里有一些解决问题的技巧和其他使代码更好的技巧。现在,我不完全确定你的确切问题是什么,但我相信如果是平局,你想知道如何重新开始比赛

将游戏放入一个
while
循环,该循环等于True。如果是平局,则使用
continue
语句(如果不确定是什么,请查找)重新启动while循环。在while循环结束时,放置
中断

我不熟悉堆栈溢出,否则我会发布带有修订的代码以使其更容易理解。此外,您不需要太多的
elif
语句来检查它是否是平局。一个简单的
elif playerChoice==computerChoice:
就可以了

如果您还没有学会如何生成函数,那么应该尝试通过生成自己的函数来简化代码。如果你还没有学会这一点,那么学习并不难,如果比赛打成平局,这也可能有助于重新开始比赛


另一个我仍在研究的技巧是在代码中添加大量注释。我无法计算在一个月左右的时间里,我返回了多少程序,我困惑地看着它,问一些函数或语句的目的是什么。希望我是有帮助的(我第一次发布关于堆栈溢出的帖子)。

我认为您要做的是缩短代码
也许不要尝试使用那么多,如果有的话,考虑下面的第一个:

r   0
s   1
p   2

you     player 2
Win
r   -   s           =   -1
s   -   p           =   -1
p   -   r           =    2

Lose
s   -   r           =    1
p   -   s           =    1
r   -   p           =   -2 

Draw
s   -   s           =    0
p   -   p           =    0
r   -   r           =    0

当你用他们的选择减去你的选择时,你会得到一组值。因此,获胜只会得到-1和2。

您需要将代码封装在名为
play()
的函数定义中,或者类似的函数定义中。在接近尾声时:

answer = True

def play():
    # your code

    if playerChoice == computerChoice: 
        print "It's a draw, want to try again?, please type YES or NO: "
        # use a dictionary to convert; default answer is False
        answer = {'YES' : True, 'NO' : False}.get(input(), False)

if answer:
    play()
这一切都深深植根于我们的思想。包括Python在内的许多语言都提供函数、类、模块、if-else和其他控制结构,允许您控制代码移动的方向。其中许多结构还可以帮助您组织代码

如果代码的输出只需要win、lose或draw,那么可以使用更高级的结构来摆脱If-else语句。在我看来,在您的情况下,它接近于最好的实现,但是当它没有封装在函数中时,它是相当丑陋的


这里有一种不同的方法可以使用python的内置程序来解决这个问题:

import random

newgame = True
choosing = False

playerchoice = ''
computerchoice = ''

options = ['rock', 'scissors', 'paper', 'lizard', 'spock']

def play():
    global choosing, computerchoice, computerchoice, newgame
    choosing = True

    while choosing == True: # or while choosing 
        choose()

    result = get_result(playerchoice, computerchoice)

    if result[0] == 'Draw':
        print "It's a draw, want to try again?", "Please type YES or NO:"

        # convert input into uppercase string, use dictionary to convert to boolean
        # default to False
        newgame = {'YES' : True, 'NO' : False}.get(str(input()).upper(), False)
        return # leave function back into 'while newgame' loop
    else:
        # fun code that converts the first value into the win/lose string, and combines it with the outcome of the round
        # join uses the string ', ' in between the two things we print
        values = [{True : 'You win!', False : 'You lose!'}[result[0]], result[1]]
        string = ', '.join(values)
        print string

def choose():
    global playerchoice, choosing, computerchoice

    print "rock, paper, scissors, lizard, spock. What do you choose?"
    playerchoice = input()

    if playerchoice in options: # if playerchoice == any option
        print 'you chose', playerchoice

        computerchoice = options[random.randint(0,4)] # take advantage of list indexes
        print 'computer chose', computerchoice

        choosing = False
    else: 
        print "this is not an option, please try again"

def get_result(playerchoice, computerchoice):
    results = {
               'rock' : {
                         'rock' : ('Draw',),
                         'paper' : (False, 'paper covers rock!'),
                         'scissors' : (True, 'rock crushes scissors!'),
                         'lizard' : (True, 'rock crushes lizard!'),
                         'spock' : (False, 'spock vaporizes rock!')
                        },
               'paper' : {
                          'rock' : (True, 'paper covers rock!'),
                          'paper' : ('Draw',),
                          'scissors' : (False, 'scissors cut paper!'),
                          'lizard' : (False, 'lizard eats paper!'),
                          'spock' : (True, 'paper disproves spock!')
                         },
               'scissors' : {
                             'rock' : (False, 'rock crushes scissors!'),
                             'paper' : (True, 'scissors cut paper!'),
                             'scissors' : ('Draw',),
                             'lizard' : (True, 'scissors kills lizard!'),
                             'spock' : (False, 'spock smashes scissors!')
                            },
               'lizard' : {
                           'rock' : (False, 'rock crushes lizard!'),
                           'paper' : (True, 'lizard eats paper!'),
                           'scissors' : (False, 'scissors kills lizard!'),
                           'lizard' : ('Draw',),
                           'spock' : (True, 'lizard poisons spock!')
                          },
               'spock' : {
                          'rock' : (True, 'spock vaporizes rock!'),
                          'paper' : (False, 'paper disproves spock!'),
                          'scissors' : (True, 'spock smashes scissors!'),
                          'lizard' : (False, 'lizard poisons spock!'),
                          'spock' : ('Draw',)
                         },
               }
    return results[playerchoice][computerchoice]

while newgame == True:
    play()

Python有一些非常强大的内置类型和函数,所以我故意在上面的代码中使用它们,这是荒谬的。希望您能够查看代码并找到有关Python的新知识

如果使用得当,if/else就足够了,但我希望我已经演示了一些Python可以简化复杂代码的强大方法。使用字典转换之类的小事情对于保持代码简洁非常有用。它们带来了轻微的性能开销,但如果使用Python,执行速度通常不是优先事项


编辑:避免使用全局变量,因为如果您忘记了
global
关键字^^,那么调试它们会非常困难;另一个常见的调试困难是,当一个元组中有一个元素
('Draw')
时,忘记通过添加逗号来告诉python这是一个元组…

欢迎使用StackOverflow,Lucas。请注意,我们的期望是您尝试自己解决任何给定的问题,然后详细描述您的特定问题,并包括一份报告。请看。它做错了什么?这种方法的缺点是它以有损的方式简化;蒸发、压碎和中毒已经不存在了。虽然这是一个简单的建议,但确实很有帮助,谢谢。我对堆栈溢出还不熟悉,否则我会发布带有修订的代码,以便更容易理解。新用户发布代码没有限制。你的代码版本确实更好,更整洁,我确实从你的代码中学到了一些东西,希望在不久的将来我会这样写,我不明白的是,你的打印内容怎么不需要任何括号?@LucasMatheus,这就是所谓的“语法糖”;许多编程语言都有真正通用代码的快捷方式。在python中,
print
是一个不需要括号的函数。Python还使用制表符,这样就不需要在代码周围键入大括号
{…}