0

Pешил сделать игру "Камень, Ножницы, Бумага" В итоге при ответе на input программа завершается, никак не могу понять в чем ошибка. Код:

import random
wins = 0
lose = 0
run = True

try:
    while run == True:
        bot = random.randint(1, 3)
        question = int (input ("Камень, Ножницы, Бумага (1-3): "))
        run = False
        if bot == "1" and question == "1":
            print ("Ничья")
            run = True
        elif bot == "1" and question == "2":
            print ("Проигрыш")
            print ("Очки: ",wins,lose)
            run = True
        elif bot == "1" and question == "3":
            print ("Победа")
            print ("Очки: ",wins,lose)
            run = True

        if bot == "2" and question == "1": 
            print ("Победа")
            print ("Очки: ",wins,lose)
            run = True

        elif bot == "2" and question == "2":
            print ("Ничья")
            run = True

        elif bot == "2" and question == "3":
            print ("Проигрыш")
            print ("Очки: ",wins,lose)
            run = True

        if bot == "3" and question == "1":
            print ("Проигрыш")
            print ("Очки: ",wins,lose)
            run = True

        elif bot == "3" and question == "2":
            print ("Победа")
            print ("Очки: ",wins,lose)
            run = True
        elif bot == "3" and question == "3":
            print ("Ничья")
            run = True

except ValueError:
    print ("Похоже вы ввели не число.")
    run = True
3
  • 2
    Замените все bot == "1" and question == "1" на bot == 1 and question == 1
    – S. Nick
    27 сен 2019 в 17:02
  • у вас bot и question имеют int значения, а Вы сравниваете их со строкой, сделайте так как Вам написал @S.Nick
    – Oleksandr
    2 дек 2019 в 17:00
  • Что будет, если пользователь введет число 7? Что программа должна делать в этом случае?
    – user244413
    2 дек 2019 в 17:54

1 ответ 1

0
import random
wins = 0
lose = 0
run = True

try:
    while run == True:
        bot = random.randint(1, 3)
        question = int (input ("Камень, Ножницы, Бумага (1-3): "))
        run = False
        if bot == 1 and question == 1:
            print ("Ничья")
            run = True
        elif bot == 1 and question == 2:
            print ("Проигрыш")
            run = True
            lose+=1
        elif bot == 1 and question == 3:
            print ("Победа")
            run = True
            wins+=1
        if bot == 2 and question == 1:
            print ("Победа")
            run = True
            wins+=1
        elif bot == 2 and question == 2:
            print ("Ничья")
            run = True
        elif bot == 2 and question == 3:
            print ("Проигрыш")
            run = True
            lose+=1
        if bot == 3 and question == 1:
            print ("Проигрыш")
            run = True
            lose+=1
        elif bot == 3 and question == 2:
            print ("Победа")
            run = True
            wins+=1
        elif bot == 3 and question == 3:
            print ("Ничья")
            run = True
        print ("Очки: ",wins,lose)
except ValueError:
    print ("Похоже вы ввели не число.")
    run = True
3
  • Выполнится только один блок if. Зачем вам переменные wins или lose, если достаточно лишь одной boolean переменной(isWin)?
    – Miron
    2 дек 2019 в 17:10
  • @Miron Выполнятся все if elif, в коде у nann были такие переменные, я только подправил их
    – Oleksandr
    2 дек 2019 в 17:13
  • Я имел ввиду содержимое блока(если введенное значение 1<=X<=3 ). Однако, лучше было бы исправить это. Излишний код.
    – Miron
    2 дек 2019 в 17:17

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.