Циклы while с вложенными операторами if;Устранение нескольких отпечатков и выход из цикла - PullRequest
0 голосов
/ 22 ноября 2018

Одна неприятная вещь, которая случается, - "рок", иногда не дает никаких результатов.Иногда я буду играть в игру, и она будет работать нормально, а в других случаях цикл закончится, и в ноль будет сыграно ноль игр.Если бы вы могли использовать код в программе, чтобы я мог узнать, где были мои ошибки, и после этого я был бы признателен за некоторые изменения, чтобы сделать его эффективным.Я думаю, что порядок вложенных условий внутри циклов while - это то, с чем я борюсь?Пожалуйста, извините за язык.

"""Rock, Paper, Scissors Exercise 8"""
game= input("Are you ready to ply? Y or N: ").capitalize()
user1 = input("What's your name? ")
user2 = input("What's your name? ")
p1 = input(user1 + ": Rock, Paper, Scissors? ").lower()
p2 = input(user2 + ": Rock, Paper, Scissors? ").lower()
p1_count=0
p2_count=0
games_played = 0

while game == "Y":
    if p1 == "rock":
        if p2 == "rock":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1
    elif p1 == "scissors":
        if p2 == "scissors":
            print("It\'s a tie!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
    elif p1 == "paper":
        if p2 == "paper":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1


print("Thank you " + user1 + " and " + user2 + " for playing this classic fucking 
game!")
print("With " + str(games_played) + " games played, " + "the score was " + user1 + " 
with " + str(p1_count) + " and " + user2 + " with " + str(p2_count))

1 Ответ

0 голосов
/ 22 ноября 2018
game= input("Are you ready to ply? Y or N: ").capitalize()
user1 = input("What's your name? ")
user2 = input("What's your name? ")

p1_count=0
p2_count=0
games_played = 0

while game == "Y":
    p1 = input(user1 + ": Rock, Paper, Scissors? ").lower()
    p2 = input(user2 + ": Rock, Paper, Scissors? ").lower()
    if p1 == "rock":
        if p2 == "rock":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1
    elif p1 == "scissors":
        if p2 == "scissors":
            print("It\'s a tie!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1
    elif p1 == "paper":
        if p2 == "paper":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1


print("Thank you " + user1 + " and " + user2 + " for playing this classic fucking game!")
print("With " + str(games_played) + " games played, " + "the score was " + user1 + " with " + str(p1_count) + " and " + user2 + " with " + str(p2_count))

Просто поместите эти две строки (p1 and p2) внутри цикла while, и все готово!

То, что произошло здесь, заключается в том, что вы не приняли входные данные дляследующее выполнение цикла while.Значения p1 и p2 остались постоянными.

Итак, теперь это сработает, Исправлены некоторые ошибки .. (последний elif оператор в 2-м и 3-м * elif выражениях)

...