剪刀石头布程序不起作用(Python) [英] Rock Paper Scissors Program Not Working (Python)

查看:231
本文介绍了剪刀石头布程序不起作用(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题: 程序似乎不接受输入的整数.不会增加赢/输/平局数,并且不会在调试模式下显示计算机选择

Problems: Program does not seem to accept the integers entered. Won't add to win/loss/draw count and does not display computer choice in debug mode

程序的基本设计: 编写一个程序,使用户可以在计算机上玩摇滚,剪纸,剪刀游戏. 该程序应按以下方式工作. 显示菜单:

Basics Design of the Program: Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. A menu is displayed:

得分:0胜0平0失 (D)调试以显示计算机的选择 (新游戏 (Q)uit

Score: 0 wins, 0 draws, 0 losses (D)ebug to show computer's choice (N)ew game (Q)uit

如果用户输入"Q"或"q",程序将结束.对于新游戏,为"N"或"n",对于调试模式为"D"或"d",否则将导致显示错误消息.

If the user enters "Q" or "q" the program would end. "N" or "n" for a new game, "D" or "d" for debug mode, anything else would cause an error message to be displayed.

  1. 游戏开始时,会生成1到3范围内的随机数.如果数字为1,则计算机选择了Rock.如果数字为2,则计算机已选择纸张.如果数字为3,则计算机已选择剪刀. (除非我们处于"D"调试模式,否则请不要显示计算机的选择.)
  2. 用户在键盘上输入他或她选择的"1-rock","2-paper"或"3-scissors".
  3. 显示计算机的选择.
  4. 根据以下规则选出获奖者: •如果一个玩家选择岩石,而另一个玩家选择剪刀,则岩石获胜. (石头砸了剪刀.) •如果一个玩家选择剪刀,而另一个玩家选择纸,则剪刀获胜.(剪刀剪纸.) •如果一个玩家选择纸牌,而另一个玩家选择石头,则纸牌获胜. (纸包裹着石头.) •如果两个玩家做出相同选择,则游戏为平局.
  5. 您的程序将始终保持获胜,失败和平局的总数.
  6. 重新显示菜单并重复游戏循环.
  1. When a game begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet unless we are in "D"ebug mode.)
  2. The user enters his or her choice of "1-rock", "2-paper", or "3-scissors" at the keyboard.
  3. The computer's choice is displayed.
  4. A winner is selected according to the following rules: • If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) • If one player chooses scissors and the other player chooses paper, then scissors wins.(Scissors cuts paper.) • If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.) • If both players make the same choice, the game is a draw.
  5. Your program would keep a running total of the number of wins, loses and draws.
  6. Re-display the menu and repeat the game loop.

我的程序:

import random

def main():

    continuing = "y"

    win = 0
    lose = 0
    draw = 0

    while continuing == "y":
        print("Score:", win,"wins,", draw, "draws,", lose,"losses")
        print("(D)ebug to show computer's choice")
        print("(N)ew game")
        print("(Q)uit")

        choice = input(" ")

        if choice == "n" or choice == "N":
            win, draw, lose = playgame(win, draw, lose)

        elif choice == "d" or choice == "D":
            win, draw, lose = playgame2(win, draw, lose)

        elif choice == "q" or choice == "Q":
            break


def playgame(win, draw, lose):

    computer = random.randint(1,3)
    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")

    if computer == 1 and player == 2:
        Score = "You won"
        win += 1

    elif computer == 1 and player == 3:
        Score = "You lost"
        lose += 1

    elif computer == 2 and player == 1:
        Score = "You lost"
        lose += 1

    elif computer == 2 and player == 3:
        Score = "You won"
        win += 1

    elif computer == 3 and player == 1:
        Score = "You won"
        win += 1

    elif computer == 3 and player == 2:
        Score = "You lost"
        lose += 1

    elif computer == player:
        Score = "Draw"
        draw += 1

    return (win, draw, lose)

def playgame2(win, draw, lose):

    computer = random.randint(1, 3)
    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")

    if computer == 1 and player == 2:
        Score = "You won"
        print("Computer chose rock")
        win += 1

    elif computer == 1 and player == 3:
        Score = "You lost"
        print("Computer chose rock")
        lose += 1

    elif computer == 2 and player == 1:
        Score = "You lost"
        print("Computer chose paper")
        lose += 1

    elif computer == 2 and player == 3:
        Score = "You won"
        print("Computer chose paper")
        win += 1

    elif computer == 3 and player == 1:
        Score = "You won"
        print("Computer chose scissors")
        win += 1

    elif computer == 3 and player == 2:
        Score = "You lost"
        print("Computer chose scissors")
        lose += 1

    elif computer == player:
        Score = "Draw"
        print("Computer chose the same as you")
        draw += 1

    return (win, draw, lose)


main()           

推荐答案

我不是Pythonista,但猜测是

I'm no Pythonista, but at a guess, input returns strings, and you'll need to convert to integer before comparing to the computer's int.

我还认为您在干燥代码中缺少窍门-您应该能够有一个playgame方法,该方法带有一个附加的布尔参数debugmode,该参数不是直接调用print而是调用间接方法,例如:

I also think you are missing a trick in DRYing up your code - you should be able to have a single playgame method, which takes an additional boolean parameter debugmode, which instead of calling print directly, calls an indirection, e.g.:

def debugPrint(debugString, debugMode)
     if debugMode
         print(debugString)

希望这有意义吗?

这篇关于剪刀石头布程序不起作用(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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