Python - Воспроизведение в то время как игра Loop Dice Roll - PullRequest
0 голосов
/ 19 октября 2019

Как мне заставить цикл ниже воспроизводиться, если пользователь вводит неверный ответ после того, как его спросили, хотят ли они снова бросить кубик?

Я не могу заставить его работать, не связавшись спока цикл. Вот что у меня пока что


# Ask the player if they want to play again

another_attempt = input("Roll dice again [y|n]?")

while another_attempt == 'y':

        roll_guess = int(input("Please enter your guess for the roll: "))
        if roll_guess == dicescore :
            print("Well done! You guessed it!")
            correct += 1
            rounds +=1
            if correct >= 4:
        elif roll_guess % 2 == 0:
            print("No sorry, it's", dicescore, "not", roll_guess)
            incorrect += 1
            rounds +=1
        else:
            print("No sorry, it's ", dicescore, " not ", roll_guess, \
                    ". The score is always even.", sep='')
            incorrect += 1
            rounds +=1
        another_attempt = input('Roll dice again [y|n]? ')

if another_attempt == 'n':
    print("""Game Summary""")


else:
    print("Please enter either 'y' or 'n'.")

Ответы [ 2 ]

0 голосов
/ 19 октября 2019

Я бы посоветовал вам сделать это с двумя циклами while и использовать функции, чтобы сделать логику кода более понятной.

def play_round():
    # Roll dice
    # Compute score
    # Display dice
    # Get roll guess

def another_attempt():
    while True:
        answer = input("Roll dice again [y|n]?")
        if answer == 'y':
            return answer
        elif answer == 'n':
            return answer
        else:
            print("Please enter either 'y' or 'n'.")

def play_game():
    while another_attempt() == 'y':
        play_round()
    # Print game summary
0 голосов
/ 19 октября 2019

удалите оператор if else из вашего цикла while внизу, а из цикла while добавьте:

another_attempt = input('Roll dice again [y|n]? ')
     if another_attempt != 'n' or another_attempt == 'y':
        another_attempt = 'y'
     elif another_attempt == 'n':
         print("""Game Summary
===========

You played""", rounds, """games
|--> Number of correct guesses:""", correct, """
|--> Number of incorrect guesses:""", incorrect, """
Thanks for playing!""")
         exit()


...