当用户第一次按“ctrl + D”时如何退出python程序中间?

时间:2014-12-11 10:02:29

标签: python exit

    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")

人类玩家也可以在任何时候按Control-D序列结束游戏。

我不知道代码是什么以及在哪里使用 这是一个摇滚剪刀的样本

1 个答案:

答案 0 :(得分:0)

我认为你应该在自己的函数中处理它(参见get_input),这样你就不必处理代码中的错误(尝试 - 除了额外的缩进......一团糟)。

例如:

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")