2

我正在做一个石头剪刀布游戏。除了赢/输/领带计数器之外,一切似乎都运行良好。我查看了人们在这里发布的其他一些游戏,但我仍然无法让我的工作。我觉得我很接近,但我就是无法得到它!感谢您的帮助。这是我第一次在这里发帖,如果我搞砸了格式,我很抱歉。

我编辑了代码,但仍然无法让程序在不使用全局变量的情况下识别计数器。在我编辑的某个时刻,我设法让它把所有东西都算作平局……我不知道怎么回事,我在编辑过程中把它弄丢了。哈哈。-再次感谢大家!

这是我运行程序时得到的结果:

Prepare to battle in a game of paper, rock, scissors!
Please input the correct number according
to the object you want to choose.
Select rock(1), paper(2), or scissors(3): 1
Computer chose PAPER .
You chose ROCK .
You lose!
Play again? Enter 'y' for yes or 'n' for no. y
Prepare to battle in a game of paper, rock, scissors!
Please input the correct number according
to the object you want to choose.
Select rock(1), paper(2), or scissors(3): 2
Computer chose PAPER .
You chose PAPER .
It's a tie!
Play again? Enter 'y' for yes or 'n' for no. y
Prepare to battle in a game of paper, rock, scissors!
Please input the correct number according
to the object you want to choose.
Select rock(1), paper(2), or scissors(3): 3
Computer chose SCISSORS .
You chose SCISSORS .
It's a tie!
Play again? Enter 'y' for yes or 'n' for no. n
Your total wins are 0 .
Your total losses are 0 .
Your total ties are 0 .

#import the library function "random" so that you can use it for computer 
#choice 
import random 

#define main 
def main(): 
    #assign win, lose, and tie to zero for tallying
    win = 0 
    lose = 0 
    tie = 0 

    #control loop with 'y' variable 
    play_again = 'y' 

    #start the game 
    while play_again == 'y': 
        #make a welcome message and give directions 
        print('Prepare to battle in a game of paper, rock, scissors!') 
        print('Please input the correct number according') 
        print('to the object you want to choose.') 

        #Get the player and computers choices and 
        #assign them to variables 
        computer_choice = get_computer_choice() 
        player_choice = get_player_choice() 

        #print choices 
        print('Computer chose', computer_choice, '.') 
        print('You chose', player_choice, '.') 

        #determine who won 
        winner_result(computer_choice, player_choice) 

        #ask the user if they want to play again 
        play_again = input("Play again? Enter 'y' for yes or 'n' for no. ") 

    #print results 
    print('Your total wins are', win, '.') 
    print('Your total losses are', lose, '.') 
    print('Your total ties are', tie, '.') 

#define computer choice 
def get_computer_choice(): 
    #use imported random function from library 
    choice = random.randint(1,3) 

    #assign what the computer chose to rock, paper, or scissors 
    if choice == 1: 
        choice = 'ROCK' 
    elif choice == 2: 
        choice = 'PAPER' 
    else: 
        choice = 'SCISSORS' 

    #return value 
    return choice 

#define player choice 
def get_player_choice(): 
    #assign input to variable by prompting user 
    choice = int(input("Select rock(1), paper(2), or scissors(3): ")) 

    #Detect invalid entry
    while choice != 1 and choice != 2 and choice != 3: 
        print('The valid numbers are rock(type in 1), paper(type in 2),') 
        print('or scissors(type in 3).') 
        choice = int(input('Enter a valid number please: ')) 

    #assign what the player chose based on entry 
    if choice == 1: 
        choice = 'ROCK' 
    elif choice == 2: 
        choice = 'PAPER' 
    else: 
        choice = 'SCISSORS' 

    #return value 
    return choice 

#determine the winner from the variables 
def winner_result(computer_choice, player_choice): 
    #if its a tie, add 1 to tie variable and display message 
    if computer_choice == player_choice:
        result = 'tie'
        print("It's a tie!")

    #if its a win, add to win tally and display message 
    elif computer_choice == 'SCISSORS' and player_choice == 'ROCK':
        result = 'win'
        print('ROCK crushes SCISSORS! You win!')
    elif computer_choice == 'PAPER' and player_choice == 'SCISSORS': 
        result = 'win'
        print('SCISSORS cut PAPER! You win!')
    elif computer_choice == 'ROCK' and player_choice == 'PAPER': 
        result = 'win'
        print('PAPER covers ROCK! You win!')

    #if it does not match any of the win criteria then add 1 to lose and 
    #display lose message 
    else: 
        result = 'lose'
        print('You lose!')

def result(winner_result,player_choice, computer_choice):

    # accumulate the appropriate winner of game total
    if result == 'win':
        win += 1
    elif result == 'lose':
        lose += 1
    else:
        tie += 1
    return result

main() 
4

4 回答 4

2

您的winner_result函数在增加获胜计数器之前返回。如果您return从中删除所有语句,则应更新计数器。return无论如何都不需要这些语句,因为该if/elif/else结构确保只执行一种可能的结果。

正如 Junuxx 在评论中所说,您还需要winner_result正确地为变量赋值,即,winner_result = 'win'而不是winner_result == 'win'. 我还会重命名winner_result变量或函数,因为两者使用相同的名称会令人困惑。

并且win/lose/tie变量当前是本地的,这意味着main并且winner_result将拥有这些变量的自己的副本,因此main的值将始终为零。您可以做的是使它们成为全局变量:在全局范围内(在任何函数之外)将它们分配为零,并global win, lose, tie在函数内添加行winner_result

于 2012-10-29T17:22:40.360 回答
0

显然这个问题已经有几年了,但是当我在寻找相同的信息时它就出现了。如果有人感兴趣,这是我的代码。

#! usr/bin/python3
import random


def game():

    computer_count = 0
    user_count = 0

    while True:
        base_choice = ['scissors', 'paper', 'rock']
        computer_choice = random.choice(base_choice)

        user_choice = input('(scissors, paper, rock) Type your choice: ').strip().lower()
        print()
        computer_wins = 'The computer wins!'
        you_win = 'You win!'

        print(f'You played {user_choice}, the computer played {computer_choice}')
        if user_choice == 'scissors' and computer_choice == 'rock' or \
           user_choice == 'paper' and computer_choice == 'scissors' or \
           user_choice == 'rock' and computer_choice == 'paper':
            print(computer_wins)
            computer_count += 1

        elif user_choice == 'rock' and computer_choice == 'scissors' or \
            user_choice == 'scissors' and computer_choice == 'paper' or \
                user_choice == 'paper' and computer_choice == 'rock':
                print(you_win)
                user_count += 1

        else:
            if user_choice == computer_choice:
                print('Its a draw!')
                computer_count += 1
                user_count += 1

        print(f'Computer: {computer_count} - You: {user_count}')
        print()


game()
于 2019-03-28T19:49:24.670 回答
0

我试图做同样的项目,我找到了一个适合我的解决方案。

from random import randint

win_count = 0
lose_count = 0
tie_count = 0

# create a list of play options
t = ["Rock", "Paper", "Scissors"]

# assign a random play to the computer
computer = t[randint(0, 2)]

# set player to false
player = False

print()
print("To stop playing type stop at any time.")
print()
while player == False:
    # set player to True
    player = input("Rock, Paper or Scissors? ")
    if player.lower() == "stop":
        print()
        print(
            f"Thanks for playing! Your final record was {win_count}-{lose_count}-{tie_count}")
        print()
        break
    if player.title() == computer:
        print()
        print("Tie!")
        tie_count += 1
        print()
    elif player.title() == "Rock":
        if computer == "Paper":
            print()
            print(f"You lose. {computer} covers {player.title()}.")
            lose_count += 1
            print()
        else:
            print()
            print(f"You win! {player.title()} smashes {computer}.")
            win_count += 1
            print()
    elif player.title() == "Paper":
        if computer == "Scissors":
            print()
            print(f"You lose. {computer} cuts {player.title()}.")
            lose_count += 1
            print()
        else:
            print()
            print(f"You win!, {player.title()} covers {computer}.")
            win_count += 1
            print()
    elif player.title() == ("Scissors"):
        if computer == "Rock":
            print()
            print(f"You lose. {computer} smashes {player.title()}.")
            lose_count += 1
            print()
        else:
            print()
            print(f"You win! {player.title()} cuts {computer}.")
            win_count += 1
            print()
    else:
        print()
        print("Sorry, we couldn't understand that.")
        print()
# player was set to True, but we want it to be false to continue loop
print(f"Your record is {win_count}-{lose_count}-{tie_count}")
print()
player = False
computer = t[randint(0, 2)]
于 2020-10-24T21:21:22.927 回答
-1

这有效(有点)页面顶部的代码有很多问题。首先,评分不起作用。其次,没有任何缩进意味着其他 def 函数内部的任何内容都不起作用。第三,在第一个 def main 语句中引用了其他 def 函数,这导致 Python 显示无效语法,因为 Python 不知道其他函数,因为它们在引入 python 之前被引用。

于 2022-02-10T17:40:59.577 回答