0

Basically in my code I have a bit where you have to click 1 then enter then 2 then enter and I want to make it so when I press 1 I don't have to click the enter button. So all I have to do is press 1 and then it will roll the dice.

I have done a quick research and can't seem to find how to / what to type in on the internet (if there is a specific thing I need to write)

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
glibdud
  • 7,550
  • 4
  • 27
  • 37
  • 6
    Possible duplicate of [raw\_input in python without pressing enter](https://stackoverflow.com/questions/3523174/raw-input-in-python-without-pressing-enter) – Isma Feb 14 '19 at 15:36
  • Or https://stackoverflow.com/questions/37726138/disable-buffering-of-sys-stdin-in-python-3 – Thomas Feb 14 '19 at 15:37
  • @Thomas i've just tried using those sites and the code when I paste it into a new file, it does not work. – Stuart Little Feb 14 '19 at 15:51
  • @Isma i've just tried using those sites and the code when I paste it into a new file, it does not work. – Stuart Little Feb 14 '19 at 15:51

1 Answers1

0

You can use the readchar package. The readchar method of this package will block the console and wait for only one character and then output this character.

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