石头布剪刀游戏的错误输入行

2024-05-14 00:29:32 发布

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

我有一个坏的输入行上的一些行,我不知道如何解决它。我试着让它工作了一段时间却一无所获。我想我少了一个结肠,但我不知道在哪里。你知道吗

编辑:我让这行工作,但'你的选择'没有被定义在第16行

我试过结肠,但没用。你知道吗

import random

def comInput():

    yourChoice = input("Rock, Paper, Scissors?")
    comChoice = random.randint(1,3)
    if comChoice == 1
        comChoice = "Rock"
    elif comChoice == 2
        comChoice = "Paper"
    elif comChoice == 3
        comChoice = "Scissors"
comInput()
def results():
    if(yourChoice == comChoice):
        print("It's a tie!")

    elif(yourChoice == "Rock" and comChoice == "Paper"):
        print("You Lose!")
    elif(yourChoice == "Rock" and comChoice == "Scissors"):
        print("You Win!")
    elif(yourChoice == "Paper" and comChoice == "Rock"):
        print("You Win!")
    elif(yourChoice == "Paper" and ComChoice == "Scissors"):
        print("You Lose!")
    elif(yourChoice == "Scissors" and comChoice == "Rock"):
        print("You Lose!")
    elif(yourChoice == "Scissors" and comChoice == "Paper"):
        print("You Win!")
results()


Tags: andyoudefrandomwinpaperprintscissors
2条回答

这些行中缺少:

if comChoice == 1:
    comChoice = "Rock"
elif comChoice == 2:
    comChoice = "Paper"
elif comChoice == 3:
    comChoice = "Scissors"

对于您的程序,需要将comInput()结果传递给results()。见下例:

import random


def comInput():
    yourChoice = input("Rock, Paper, Scissors?")
    comChoice = random.randint(1, 3)
    if comChoice == 1:
        comChoice = "Rock"
    elif comChoice == 2:
        comChoice = "Paper"
    elif comChoice == 3:
        comChoice = "Scissors"

    return yourChoice, comChoice


def results(yourChoice, comChoice):
    if (yourChoice == comChoice):
        print("It's a tie!")

    elif (yourChoice == "Rock" and comChoice == "Paper"):
        print("You Lose!")
    elif (yourChoice == "Rock" and comChoice == "Scissors"):
        print("You Win!")
    elif (yourChoice == "Paper" and comChoice == "Rock"):
        print("You Win!")
    elif (yourChoice == "Paper" and comChoice == "Scissors"):
        print("You Lose!")
    elif (yourChoice == "Scissors" and comChoice == "Rock"):
        print("You Lose!")
    elif (yourChoice == "Scissors" and comChoice == "Paper"):
        print("You Win!")


results(*comInput())

在修改代码时,它看起来是用Java模式风格指南编写的。看看这个问题What is the naming convention in Python for variable and function names?,并尝试改进您的代码!你知道吗

下面一行有一个拼写错误,ComChoice是大写的。你知道吗

elif(yourChoice == "Paper" and ComChoice == "Scissors"):

查看下面的链接,了解初学者的游戏方法。 https://thehelloworldprogram.com/python/python-game-rock-paper-scissors/

祝你好运!你知道吗

相关问题 更多 >