如何阻止我的Python游戏代码在第34行之后关闭?

2024-06-03 00:24:47 发布

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

我三天前才开始学编程。我决定学习Python 3.7.2版。今天我写了这段代码,试图创建一个游戏,在这个游戏中,你输入一个用户名,然后用石头、布、剪刀来对付一个机器人。但是,在输入用户名(第2-4行)之后,Python shell就关闭了。我正在运行的程序,在我的资源管理器,视窗10点击文件。我想这是一个简单的初学者的错误,有人能帮忙吗?你知道吗

我尝试在第4行添加input()。当我这样做时,Python shell在输入用户名后不会关闭。它什么都不做,只是换行而已。当我在新行中键入一些内容并按Enter键时,shell关闭。你知道吗

这是我的密码:

import random
print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
usn = input()

def game(choice):
  options = ['rock', 'paper', 'scissors']
  bot = random.choice(options)
  if choice == 'rock' and bot == 'scissors':
    print(usn + ' played rock, I played scissors. You won. Nice!')
  elif choice == 'rock' and bot == 'paper':
    print(usn + ' played rock, I played paper. You lost. Haha, loser!')
  elif choice == 'paper' and bot == 'scissors':
    print(usn + ' played paper, I played scissors. You lost. Haha, loser!')
  elif choice == 'paper' and bot == 'rock':
    print(usn + ' played paper, I played rock. You won. Nice!')
  elif choice == 'scissors' and bot == 'paper':
    print(usn + ' played scissors, I played paper. You won. Nice!')
  elif choice == 'scissors' and bot == 'rocks':
    print(usn + ' played scissors, I played rocks. You lost. Haha, loser!')
  elif choice == bot:
    print("It's a draw, dang it!")

def again(x):
    while True:
        if x == 'Yes':
            game(input('Rock, Paper or Scissors?'))
            again(input('Want to play again? Yes or No'))
        else:
            print('Goodbye. Press Enter to exit.')
            input()

期望:我期望在输入用户名后,我可以输入石头、布或剪刀,然后游戏就可以玩了。 实际结果:pythonshell在输入用户名后关闭。你知道吗


Tags: andtoyou游戏inputbot用户名paper
3条回答

def game(choice):创建一个名为game的函数。您需要告诉python用一个参数执行这个函数:choice 就像这样:game(usn)

您也需要调用game,尽管此时需要声明game。你知道吗

我在这里重构了一些东西,让它们对用户来说更方便一些。你知道吗

  • game中使用的参数名是usernameuser_choice,这样就不会影响全局名称(或意外地使用它们)。你知道吗
  • again()递归函数被一个while循环所取代,当用户说他们不想再玩时,这个循环被break终止。你知道吗
  • RPS和问题输入都是小写和剥离的,所以用户输入“NO”、“NO”、“pAPeR”等就可以了。你知道吗

我建议考虑的下一件事是如何简化game函数中的重复。:)

import random


def game(username, user_choice):
    options = ['rock', 'paper', 'scissors']
    bot = random.choice(options)
    if user_choice == 'rock' and bot == 'scissors':
        print(username + ' played rock, I played scissors. You won. Nice!')
    elif user_choice == 'rock' and bot == 'paper':
        print(username + ' played rock, I played paper. You lost. Haha, loser!')
    elif user_choice == 'paper' and bot == 'scissors':
        print(username + ' played paper, I played scissors. You lost. Haha, loser!')
    elif user_choice == 'paper' and bot == 'rock':
        print(username + ' played paper, I played rock. You won. Nice!')
    elif user_choice == 'scissors' and bot == 'paper':
        print(username + ' played scissors, I played paper. You won. Nice!')
    elif user_choice == 'scissors' and bot == 'rocks':
        print(username + ' played scissors, I played rocks. You lost. Haha, loser!')
    elif user_choice == bot:
        print("It's a draw, dang it!")


print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
name = input()

while True:  # do forever, unless `break`ed out of (or an exception occurs)
    choice = input('Rock, Paper or Scissors?').lower().strip()
    game(name, choice)
    if input('Want to play again? Yes or No').lower().strip() == 'no':
        print('Goodbye. Press Enter to exit.')
        input()
        break  # break out of the "while true" loop

第一个问题是,您在收到用户名后没有调用任何函数,我将尝试以下操作(注意,我在代码中添加了一些注释):

import random
print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
usn = input()

def game(choice):
  options = ['rock', 'paper', 'scissors']
  bot = random.choice(options)
  if choice == 'rock' and bot == 'scissors':
    print(usn + ' played rock, I played scissors. You won. Nice!')
  elif choice == 'rock' and bot == 'paper':
    print(usn + ' played rock, I played paper. You lost. Haha, loser!')
  elif choice == 'paper' and bot == 'scissors':
    print(usn + ' played paper, I played scissors. You lost. Haha, loser!')
  elif choice == 'paper' and bot == 'rock':
    print(usn + ' played paper, I played rock. You won. Nice!')
  elif choice == 'scissors' and bot == 'paper':
    print(usn + ' played scissors, I played paper. You won. Nice!')
  elif choice == 'scissors' and bot == 'rocks':
    print(usn + ' played scissors, I played rocks. You lost. Haha, loser!')
  elif choice == bot:
    print("It's a draw, dang it!")

# Taking this out of the function, it is going to be called right on the execution
x = 'Yes' 
while True: #here you are doing an loop, that will only end when break is called
    if x == 'Yes':
        game(input('Rock, Paper or Scissors?'))
        x = input('Want to play again? Yes or No')
    else:
        print('Goodbye. Press Enter to exit.')
        input()
        break

相关问题 更多 >