无需Enter键即可接受键盘输入

问题描述 投票:0回答:1

基本上在我的代码中我有一点你必须单击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
python
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.