Rock Paper Scissors在Python中提供帮助 [英] Rock Paper Scissors function help in Python

查看:56
本文介绍了Rock Paper Scissors在Python中提供帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习Python,我正在为我的作业写一个基本的Rock Paper Scissors程序。游戏打算继续进行10轮,同时跟踪玩家和计算机之间的分数。

 导入随机

def welcome_prompt():$ b $ (规则:岩石节拍剪刀,剪刀节拍纸,纸节拍摇滚)
$ b $ def get_player_move():$ b $(ROCKER PAPER SCISSORS in PYTHON Assignment b打印('Round'+ str(round))
print(请播放以下内容之一)
get_player_move = raw_input(1)[R] ock,2)[P] aper, 3)[S] cissors:)

if get_player_move ==(R):
print(You used Rock!)
return 1

elif get_player_move ==(P):
print(You used Paper!)
return 2

elif get_player_move ==(S) :
print(You used Scissors!)
return 3

else:
print输入无效,请使用大写初始值(R,P,S)
return get_player_move()
$ b $ def get_computer_move():
get_computer_move = random.randint(1,3)

如果get_computer_move == 1:
print(Computer used Rock!)
return 1

elif get_computer_move == 2:
print(Computer Used Paper!)
return 2

elif get_computer_move == 3:
print(计算机使用剪刀!)
返回3



def compare_moves(get_player_move,get_computer_move):
#Rock = 1
#Paper如果(get_player_move == 1和get_computer_move == 1)或(get_player_move == 2和get_computer_move == 2)或(get_player_move == 3和get_computer_move == 2)= 2
#Scissors = 3

== 3):
print(这是一个领带!)
返回0

elif(get_player_move == 1和get_computer_move == 3)或(get_player_move == 2和get_computer_move == 1)或(get_player_move == 3和get_computer_move == 2):
print(You win the round!)
return 1

el如果(get_player_move == 1和get_computer_move == 2)或(get_player_move == 2和get_computer_move == 3)或(get_player_move == 3和get_computer_move == 1):
print(你失败了! )
返回-1

elif(get_player_move == 4):
print(您没有输入正确的输入,计算机获得免费的胜利)
返回-1


#游戏程序

player_score = 0
comp_score = 0
round = 0


welcome_prompt()

('Round'+ str(round))
while round< 10:
round = round + 1
get_player_move()
get_computer_move()
compare_moves(get_player_move,get_computer_move)
$ b $ if compare_moves == 1:
player_score = player_score + 1
print'Player Score'+ str(player_score)
print'Computer Score'+ str(player_score)
elif compare_moves == -1:
comp_score = comp_score + 1
print'Player Score'+ str(player_score)
print'Computer Score'+ str(player_score)

printGame Over

首先,我无法使用compare_move函数来调用get_player_move和get_computer_move的返回值。游戏可以没有任何错误地运行,但它只是完全跳过比较/评分组件。其次,在get_player_move函数中,当我输入一个无效的输入时(例如:等等) )来测试raw_input,它会给出一个错误。

  Traceback(最近一次调用的最后一个):
文件C:\Python27\Rock Paper Scissors.py ,第85行,在< module>
get_player_move()
文件C:\Python27\Rock Paper Scissors.py,第32行,在get_player_move
返回get_player_move()
TypeError:'str'对象无法调用

那么在输入无效输入后,如何让函数再次请求正确的raw_input ,而不中断while循环?



解释非常感谢,谢谢

解决方案

在函数 get_player_move()中有一个局部变量 get_player_move ;



重命名 get_player_move 局部变量。

>

因此,而不是:

  get_player_move = raw_input(1)[R 2)[P] aper,3)[S] cissors:)

  move = raw_input(1)[R] ock,2)[P] aper,3)[S] cissors: )

或许。

用户输入,但最好不要依赖递归。用户可能会永远点击'C',然后你的程序会崩溃,出现 RuntimeError:超过最大递归深度。相反,使用循环更简单:

  true:
move = raw_input(1)[R] ,2)[P] aper,3)[S] cissors:)
if move ==R:
print(You used Rock!)
return 1

#等等

其他:
print输入无效,请使用大写的初始值(R,P,S)

因为您在进行正确选择时从函数返回,所以循环也会自动退出。如果你打到最后并且打印出无效输入,那么 while True 循环从顶部开始,而用户会被要求再次输入一个选项。



下一步:尽管您的函数返回一个选项(整数),但您永远不会存储该返回值。您必须将其存储在您称为函数的位置:

$ $ p $ player_move = get_player_move()
computer_move = get_computer_move()
result = compare_moves(player_move,computer_move)

if result == 1:

请注意,它不是保存返回值的函数名称;它是一个单独的变量。例如,返回的 get_player_move()会分配 player_move



然后,您可以将这些返回的值传递给 compare_moves();它还会返回一个结果,在这里存储在 result 中作进一步比较。


I just started learning Python, and I am trying to write a basic Rock Paper Scissors program for my assignment. The game is intended to go on for 10 rounds, while keeping track the score between the player and the computer. I have two specific problems with it.

import random

def welcome_prompt():
    print ("ROCKER PAPER SCISSORS in PYTHON Assignment")        
    print ("Rules: Rocks beats Scissors, Scissors beats Paper, Paper beats Rock")

def get_player_move():
    print ('Round ' + str(round))        
    print ("Please play one of the following")
    get_player_move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")

    if get_player_move == ("R"):
        print ("You used Rock!") 
        return 1              

    elif get_player_move == ("P"):        
        print ("You used Paper!")
        return 2

    elif get_player_move == ("S"):
        print ("You used Scissors!")
        return 3

    else:
        print "Invalid input, please use capitalized initial (R,P,S)"
        return get_player_move()

def get_computer_move():
        get_computer_move = random.randint(1,3)

        if get_computer_move == 1:
            print ("Computer used Rock!")
        return 1

    elif get_computer_move == 2:
        print ("Computer used Paper!")
        return 2

    elif get_computer_move == 3:
        print ("Computer used Scissors!")
        return 3



def compare_moves(get_player_move, get_computer_move):
# Rock = 1
# Paper = 2
# Scissors = 3

    if (get_player_move ==  1 and get_computer_move == 1) or (get_player_move == 2 and get_computer_move == 2) or (get_player_move == 3 and get_computer_move == 3):
    print ("It's a tie!")
    return 0  

    elif (get_player_move == 1 and get_computer_move == 3) or (get_player_move == 2 and get_computer_move == 1) or (get_player_move == 3 and get_computer_move == 2):
    print ("You win the round!")
    return 1

    elif (get_player_move == 1 and get_computer_move == 2) or (get_player_move == 2 and get_computer_move == 3) or (get_player_move == 3 and get_computer_move == 1):
    print ("You lose the round!")
    return -1

    elif (get_player_move == 4):
    print ("You didn't put in correct input, computer gets a free win")
    return -1


# Game Program

player_score = 0
comp_score = 0
round = 0


welcome_prompt()

('Round ' + str(round))
while round< 10:
    round = round + 1
    get_player_move()
    get_computer_move()
    compare_moves(get_player_move, get_computer_move)

    if compare_moves == 1:
        player_score = player_score + 1
        print 'Player Score'+ str(player_score)
        print 'Computer Score'+ str(player_score)
    elif compare_moves == -1:
        comp_score = comp_score + 1
        print 'Player Score'+ str(player_score)
        print 'Computer Score'+ str(player_score)        

print "Game Over"

Firstly, I can't get the compare_move function to recall the returned values from both get_player_move and get_computer_move. The game can run without any error, but it just skips the comparison/ score component completely. I am still a bit iffy with the basics, so not exactly sure what is missing.

Secondly, in the get_player_move function, when I enter an invalid input (example: blah) to test the raw_input, it gives an error.

Traceback (most recent call last):
  File "C:\Python27\Rock Paper Scissors.py", line 85, in <module>
    get_player_move()
  File "C:\Python27\Rock Paper Scissors.py", line 32, in get_player_move
    return get_player_move()
TypeError: 'str' object is not callable

So how do you make a function to asks for the correct raw_input again after entering invalid input, without interrupting the while loop?

Explanation is greatly appreciated, thank you

解决方案

You have a local variable get_player_move inside the function get_player_move(); you cannot then still use the function name (a global).

Rename the get_player_move local variable.

So, instead of:

get_player_move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")

use:

move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")

perhaps.

To get user input, it's best not to rely on recursion, however. The user could hit 'C' forever and then your program would crash with an RuntimeError: maximum recursion depth exceeded. It's easier to use a loop instead:

while True:
    move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")
    if move == "R":
        print ("You used Rock!") 
        return 1              

    # etc.

    else:
        print "Invalid input, please use capitalized initial (R,P,S)"

Because you return from the function when a correct choice is made, the loop automatically is exited as well. If however you get to the end and Invalid input is printed, the while True loop starts at the top again and the user is asked once more to enter a choice.

Next: although your function returns a choice (an integer), you never store that return value. You must store it where you called the function:

player_move = get_player_move()
computer_move = get_computer_move()
result = compare_moves(player_move, computer_move)

if result == 1:

Note that it's not the function name that holds the return value; it's a separate variable. player_move is assigned whatever the get_player_move() returned, for example.

You can then pass these returned values to compare_moves(); it also returns a result, here stored in result for further comparisons.

这篇关于Rock Paper Scissors在Python中提供帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆