我的简单的python游戏不起作用请找出我的错误

2021-10-17 14:43:56 发布

您现在位置:Python中文网/ 问答频道 /正文

from random import randint
import sys
l2= ["Rock", "Paper", "Scissors"]

computer = l2[randint(0,2)]
wins = 0
losses = 0

player = False

while player == False:
    player = input("Rock," "Paper," "Scissors")
    if player == computer:
        print("Tie")    
    elif player == "Rock":
        if computer == "Paper":
            print("You lose")
        else:
            print("You win Congrats")
    elif player == "Paper":
        if computer == "Scissors":
            print("You lose")
        else:
            print("You win Congrats")    
    elif player == "Scissors":
        if computer == "Rock":
            print("Try again")
        else:
            print("You won")
    elif player == "Rock":
        if computer == "Scissors":
            print("You won")
    elif player == "Paper":
        if computer == "Rock":
            print("You won")
    elif player == "Scissors":
        if computer == "Paper":
            print("You won")
    else:
        print("That is not a valid play Game Over for you")
    player = False
    computer = l2[randint(0,2)] 

我的代码由于某种原因无法运行,它只在终端上显示else语句。我不知道我做错了什么。请帮助我,初学者在这里学习python和做一些简单的项目开始。你知道吗

3条回答
网友
1楼 ·

看看你的代码,大约一半的if-else案例不起作用。你的代码怎么了?代码正在运行。你可以消除一半的elif案例,因为它们永远不会执行。If和elif是连续的。如果您仍然认为您的代码不起作用,请逐个缓慢地进行调试。如中所示,将计算机设置为常量并测试所有情况。你知道吗

我复制粘贴了你的代码,它工作得很好,至少表面上的功能。删除不必要的elif:

from random import randint
import sys
l2= ["Rock", "Paper", "Scissors"]

computer = l2[randint(0,2)] 

wins = 0
losses = 0

player = False

while player == False:
    player = input("Rock,Paper,Scissors: ") #clean up the query
    print("Computer had ",computer) #You use this to debug to make sure the logic is working. 
    print("You entered ",player) # another print to make it look nicer
    if player == computer:
        print("Tie")    
    elif player == "Rock":
        if computer == "Paper":
            print("You lose")
        else:
            print("You win Congrats")
    elif player == "Paper":
        if computer == "Scissors":
            print("You lose")
        else:
            print("You win Congrats")    
    elif player == "Scissors":
        if computer == "Rock":
            print("Try again")
        else:
            print("You won")
    else:
        print("That is not a valid play Game Over for you")
    player = False
    computer = l2[randint(0,2)] 

逻辑运作得很好。祝你在游戏中添加其他功能好运。你知道吗

我建议尝试使用upper()来减少打字错误造成的失败,也可以使用exit string命令。对于您似乎想要添加的赢/输功能,只需做一个添加到赢/输号码的标志。你也可以添加其他“命令”到你的游戏与更多的如果否则的情况。祝你好运。你知道吗

网友
2楼 ·

首先,不应该在同一个问题上添加Python2.x和Python3.x标记。为了简单起见,我在3.x上尝试了你的代码,它部分工作。 第二,描述你的问题。不要只写“因为某些原因不会跑”。 第三,描述你想要达到的目标。我将为你举个例子。你知道吗

你的游戏是这样运作的:

  1. 确定计算机的功能

  2. 询问玩家做什么(部分工作)

  3. 比较播放器和计算机操作并打印结果(不起作用)

  4. 从1开始(不起作用)

第一个问题是while循环,您应该这样做:

while True:

为什么?你想要一个永无止境的循环只有当玩家输入了一个错误的动作,然后你想要强制循环结束,你用一个“break”语句来完成。你知道吗

你的第二个问题是,你使用了太多的if-else构造,你不需要那么多来处理所有的情况。下面是我的Python 3代码:

from random import randint
import sys

l2 = ["Rock", "Paper", "Scissors"]

computer = None
player = None
wins = 0
losses = 0
ties = 0

def win():
    global wins
    wins += 1 
    print("You win")

def loss():
    global losses
    losses += 1
    print("You lose")

def tie():
    global ties
    ties += 1
    print("Game ended with a tie")

while True:
    # determine computer action
    computer = l2[randint(0,2)]
    player = input("Choose your action. Possible values are Rock, Paper or Scissors.\n")
    if player not in l2:
        print("Invalid choice, game will end now.")
        print("You won {0} games and lost {1} games, {2} game(s) ended with a tie.".format(wins, losses, ties))
        # ending the loop
        break
    if player == computer:
        tie()
    elif player == "Rock":
        if computer == "Paper":
            win()
        else:
            loss()
    elif player == "Paper":
        if computer == "Scissors":
            loss()
        else:
            win()   
    elif player == "Scissors":
        if computer == "Rock":
            loss()
        else:
            win()

如果你有任何问题,问我。你知道吗

Edit:您还应该考虑将函数和主循环移动到它自己的(game)类中,这样就不需要使用丑陋的“global”语句来访问变量。你知道吗

网友
3楼 ·

我还有一个石头布剪刀的游戏

以下是一些工作代码:

import random
computer = random.choice(["rock", "paper", "scissors"])
player = input("What do you choose?")
player = player.lower()
print(f"Computer picks {computer} and Player picks {player}")
if computer == player:
    print('Game is a tie, play again!')
if player == "paper":
  if computer == "rock":
    print('Player wins!')
  if computer == "scissors":
    print('Computer wins!')
if player == "rock":
  if computer == "paper":
    print('Computer wins!')
  if computer == "scissors":
    print('Player wins!')
if player == "scissors":
  if computer == "rock":
    print('Computer wins!')
  if computer == "paper":
    print('Player wins!')

相关问题