【问题标题】:My simple python game wont work please identify my mistakes我的简单 python 游戏无法运行请指出我的错误
【发布时间】:2019-10-31 10:05:40
【问题描述】:
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并开始做一些简单的项目。

【问题讨论】:

    标签: python python-3.x python-2.7 list python-requests


    【解决方案1】:

    首先,你不应该在同一个问题上添加 python 2.x 和 python 3.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()
    

    如果您有任何问题,请提出。

    编辑:您还应该考虑将函数和主循环移动到它自己的(游戏)类中,这样就不需要丑陋的“全局”语句来访问变量。

    【讨论】:

    • 感谢您提供的信息,我对 python 有点陌生,所以我不太了解,但基础知识就是我尝试通过简单项目发展自己的原因,感谢您的帮助
    • 每个函数都有自己的作用域。默认情况下,您不能在函数内部写入全局(在模块级别定义)变量,它会在函数范围内创建一个新变量。使用 global 关键字,您可以将变量导入函数范围,然后可以更改它的值。如果您对其中一个答案感到满意,您可以将问题标记为已解决并接受正确答案。
    【解决方案2】:

    查看您的代码,大约一半的 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() 来减少因拼写错误而导致的失败,也许还可以使用退出字符串命令。对于您似乎想要添加的赢/输功能,只需做一个添加到赢/输数字的标志。如果其他情况,您还可以在游戏中添加其他“命令”。好好尝试,祝你好运。

    【讨论】:

    • 它对你有用吗?我将您所说的内容应用于我的代码,但它仍然无法正常运行我没有显示用户的任何输入,但我有一个代码,它只在终端上显示 else 语句
    • 你能发布你的代码输出吗?我不确定还有哪些正在为您打印。是的,它确实对我有用。除了在 input() 调用中输入的内容之外,您的初始代码不显示来自用户的任何输入。
    • 感谢我刚接触 python 的技巧,所以我在放学期间尝试发展自己
    • 没问题。随意接受任何答案。所有最好的学习python。如果你打算在未来的职业生涯中使用 python,那么你对 AI 很感兴趣,你可以尝试 kaggle 获得一些很酷的教程或玩弄 OpenAI 环境。
    【解决方案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!')
    

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      相关资源
      最近更新 更多