If和elif语句不工作/python无法识别

2024-05-20 15:45:56 发布

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

我试图创建一个小石头剪刀布游戏,因为我是一个初学者,但我有我的if和elif语句的问题

import random
player_score = 0
computer_score = 0
options = ['rock', 'paper', 'scissors']

def player_choice():
    input('Rock, Paper or Scissors? ')
    return player_choice
def computer_choice():
    print(random.choice(options))
    return computer_choice

ps = print('player score: ', player_score)
cs = print('computer_score: ',computer_score)

while player_score or computer_score < 10:
    player_choice()
    computer_choice()

if player_choice == 'rock' and computer_choice == 'rock':
        print('Tie')
elif player_choice == 'rock' and computer_choice == 'paper':
        print('Computer wins')
        computer_score = computer_score + 1
        print(ps)
        print(cs)
elif player_choice == 'rock' and computer_choice == 'scissors':
        print('You win')
        player_score = player_score + 1
        print(ps)
        print(cs)

似乎整个if/elif块都被忽略,没有任何内容被打印或递增。没有错误弹出,它只是简单地被忽略


Tags: andifrandomcscomputerpaperpsoptions
3条回答

您的代码有几个问题

def player_choice():
    input('Rock, Paper or Scissors? ')
    return player_choice

玩家选择未初始化。Input接受用户输入,但结果不会存储在任何位置

def get_player_choice():
    choice = input("Rock, Paper or Scissors? ")
    return choice

computer_choice执行相同的过程

其次是:

ps = print('player score: ', player_score)
cs = print('computer_score: ',computer_score)

我假设您试图构造一个包含分数的字符串。我建议

def print_score():
    print('player score: ', player_score)
    print('computer_score: ',computer_score)

相反。然后,您可以随时调用该函数以查看分数

最后,缩进不适合if-else语句。它应该在while循环中。现在,它只是要求用户输入,并在一个无休止的循环中为计算机生成一个选择

以下是我建议的所有更改:


import random
player_score = 0
computer_score = 0
options = ['rock', 'paper', 'scissors']

def get_player_choice():
    player_choice= input('Rock, Paper or Scissors? ')
    return player_choice
def get_computer_choice():
    computer_choice = random.choice(options)
    print(computer_choice)
    return computer_choice

def print_score():
    print('player score: ', player_score)
    print('computer_score: ',computer_score)

while player_score < 10  and computer_score < 10:
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()

    if player_choice == 'rock' and  computer_choice == 'rock':
        print('Tie')
    elif player_choice == 'rock' and computer_choice == 'paper':
        print('Computer wins')
        computer_score = computer_score + 1
        print_score()
    elif player_choice == 'rock' and computer_choice == 'scissors':
        print('You win')
        player_score = player_score + 1
        print_score()

请检查代码的第13行。我不明白ps = print(...)是什么意思

您的代码有几个问题,我将尝试解决所有这些问题

第一个问题与变量的命名有关。将函数命名为computer_choiceplayer_choice,然后检查它们是否等于"rock"或其他字符串。这只会返回False,因为computer_choice是一个函数,而不是字符串。我建议将函数名更改为get_computer_choice()get_player_choice()

其次,ps = print('player score: ', player_score)。我不知道你在那里想干什么ps将是None,因为print()不返回任何内容

第三,函数返回本身

def my_func():
    return my_func

将返回一个函数。对于您选择的两个函数,您要做的是:

def get_player_choice():
    player_choice = input('Rock, Paper or Scissors? ')
    return player_choice

def get_computer_choice():
    computer_choice = random.choice(options) # Set computer_choice to computers choice
    print(computer_choice)
    return computer_choice

第四,在while循环下,您正在调用函数,但没有对返回执行任何操作。改变

while player_score or computer_score < 10:
    player_choice()
    computer_choice()

while player_score or computer_score < 10:
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()

最后,if ... else语句需要在while循环下缩进,否则它们永远不会执行

相关问题 更多 >