在用户首次按下“CTRL+D”时,如何在Python程序的中间退出? 人类玩家也可以随时按Control-D顺序结束游戏。

在用户首次按下“CTRL+D”时,如何在Python程序的中间退出? 人类玩家也可以随时按Control-D顺序结束游戏。,python,exit,Python,Exit,我不知道代码是什么,在哪里使用 这是一个石头剪刀的示例我认为您应该在自己的函数中处理它,请参见get_input,这样您就不必处理代码中的所有错误,只需尝试额外的缩进。。。一团糟 例如: import random import string name=raw_input("what is your name?") taunts=['taunts1','taunts2','taunts3','taunts4'] point = inpu

我不知道代码是什么,在哪里使用
这是一个石头剪刀的示例

我认为您应该在自己的函数中处理它,请参见get_input,这样您就不必处理代码中的所有错误,只需尝试额外的缩进。。。一团糟

例如:

    import random
    import string
    name=raw_input("what is your name?")
    taunts=['taunts1','taunts2','taunts3','taunts4']
    point = input("Enters the number of points required for a win. ")
    human_p = 0
    computer_p = 0
    selection = ['rock', 'paper', "scissors"]
    
    while human_p < point and computer_p < point:
        computer = random.choice(selection)
        human = raw_input("Choose rock, paper, or scissors ? ")
        print name," selects :", human, "computer selects :", computer
        if computer==human:
            print  name+(" score is %d  computer score is %d  A draw" %(human_p, computer_p))
    
        if (computer == 'rock' and human == 'scissors') or (computer== 'paper' and human == 'rock') or(computer == 'scissors' and human == 'paper'):
            computer_p = computer_p + 1
            print name+(" score is %d  computer score is %d Computer wins" %(human_p, computer_p))
            print random.choice(taunts)
    
        if (computer == 'rock' and human == 'paper') or (computer== 'paper' and human == 'scissors') or (computer == 'scissors' and human == 'rock'):
            human_p = human_p + 1
            print (name+(" score is %d  computer score is %d Human wins" %(human_p, computer_p)))
    
    print("Final score is : %d  %d and computer %d" % (name,human_p, computer_p))
    if human_p == point:
        print ("The overall winner is %d" %name)
    elif computer_p == point:
        print ("The overall winner is Computer")

如果用户在输入提示下按Ctrl+d,将引发一个异常,您可以捕获并执行任何您想要的操作。我应该如何实现它?@khelwood:它在*nix上执行,但IIRC Windows使用Ctrl+z而不是Ctrl+d来执行EOF。因此,在输入字符串中简单地检测“\x04”可能就足够了。@PM2Ring发现得很好。谢谢,但为什么我按ctrl+d时它没有退出呢!:对我有用。也许你不生产EOFError是出于某种原因。按ctrl+d时会发生什么?返回的是什么输入?另外,我在发布答案后很快编辑了答案,请确保你有最新的代码。是的,我复制了最新的代码,但它对我不起作用,实际上什么都没有发生:你到底是如何运行这段代码的?什么操作系统?什么环境?pycharm,windows7,64位
import random
import string
import sys
import os

# handle input
def get_input(msg):
    try:
        return raw_input(msg)
    except EOFError:
        print os.linesep + "user quit." 
        sys.exit(0)


name= get_input("what is your name?")
taunts=['taunts1','taunts2','taunts3','taunts4']
point = get_input("Enters the number of points required for a win. ")
human_p = 0
computer_p = 0
selection = ['rock', 'paper', "scissors"]

while human_p < point and computer_p < point:
    computer = random.choice(selection)
    human = get_input("Choose rock, paper, or scissors ? ")
    print name," selects :", human, "computer selects :", computer
    if computer==human:
        print  name+(" score is %d  computer score is %d  A draw" %(human_p, computer_p))

    if (computer == 'rock' and human == 'scissors') or (computer== 'paper' and human == 'rock') or(computer == 'scissors' and human == 'paper'):
        computer_p = computer_p + 1
        print name+(" score is %d  computer score is %d Computer wins" %(human_p, computer_p))
        print random.choice(taunts)

    if (computer == 'rock' and human == 'paper') or (computer== 'paper' and human == 'scissors') or (computer == 'scissors' and human == 'rock'):
        human_p = human_p + 1
        print (name+(" score is %d  computer score is %d Human wins" %(human_p, computer_p)))

print("Final score is : %d  %d and computer %d" % (name,human_p, computer_p))
if human_p == point:
    print ("The overall winner is %d" %name)
elif computer_p == point:
    print ("The overall winner is Computer")