接受键盘输入而无需Enter键

时间:2019-02-14 15:35:29

标签: python

基本上在我的代码中,您需要单击1,然后输入,然后输入2,然后输入,我想要创建它,因此当我按1时,不必单击回车按钮。所以我所要做的就是按1,然后它将掷骰子。

我已经进行了快速研究,似乎无法找到如何/在互联网上键入内容(如果有需要写的特定内容)

def Game_Round1():
    while player1_dice1_round1_dice != "1":
        player1_dice1_round1_dice = input("Press 1 to roll dice!: ")
    else:
        player1_dice1_round1_answer = random.randint(1, 6)
        print("You rolled the first dice and the answer is: ", player1_dice1_round1_answer)    

    player1_dice2_round1_roll_start = input("Press 2 to roll dice 2: ")
    while player1_dice2_round1_roll_start != "2":
        player1_dice2_round1_roll_start = input("Press 2 to roll dice2!: ")
    else:
        player1_dice2_round1_answer = random.randint(1, 6)
        print("You rolled the second dice and the answer is: ", player1_dice2_round1_answer)

    p1_round1_answer = (player1_dice1_round1_answer + player1_dice2_round1_answer)

    odd_or_even_checker = p1_round1_answer % 2
    if odd_or_even_checker > 0:
        print("as your number is even you will loose 5  points ")
        p1_round1_answer -= 5
    else:
        print("as your number is even you will gain an extra 10 points ")
        p1_round1_answer += 10

1 个答案:

答案 0 :(得分:0)

您可以使用readchar软件包。该程序包的readchar方法将阻止控制台,仅等待一个字符,然后输出此字符。

import readchar

def Game_Round1():
    while player1_dice1_round1_dice != "1":
        player1_dice1_round1_dice = readchar.readchar("Press 1 to roll dice!: ")
    else:
        player1_dice1_round1_answer = random.randint(1, 6)
        print("You rolled the first dice and the answer is: ", player1_dice1_round1_answer)    

    player1_dice2_round1_roll_start = readchar.readchar("Press 2 to roll dice 2: ")
    while player1_dice2_round1_roll_start != "2":
        player1_dice2_round1_roll_start = input("Press 2 to roll dice2!: ")
    else:
        player1_dice2_round1_answer = random.randint(1, 6)
        print("You rolled the second dice and the answer is: ", player1_dice2_round1_answer)

    p1_round1_answer = (player1_dice1_round1_answer + player1_dice2_round1_answer)

    odd_or_even_checker = p1_round1_answer % 2
    if odd_or_even_checker > 0:
        print("as your number is even you will loose 5  points ")
        p1_round1_answer -= 5
    else:
        print("as your number is even you will gain an extra 10 points ")
        p1_round1_answer += 10