N
N
Nikita Chaynikovich2021-11-01 22:35:36
Python
Nikita Chaynikovich, 2021-11-01 22:35:36

How to change the value of a variable during the execution of a loop?

I'm trying to make a randomizer, but I can't display the remaining number of attempts + I don't understand why it doesn't respond to entering the correct number

import random
print('Угадай число от 1 до 12')
trys = int(input('сколько попыток хочешь? '))
rand = random.randint(0, 12)
print(rand)
for i in range(0, trys):
    dict = {1: trys - 1}
    tr1 = dict[1]
    pop = input('Угадай число ')
    if pop == rand:
        print()
        print('Правильно, было загадано число ', rand, '!')
        print()
    elif pop != rand:
        print('Не верно, попробуй ещё раз, осталось ', tr1, ' попыток')
    if trys == 0:
        print('Попытки закончились, попробуй снова')
        break

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-11-01
@Tomio

pop == rnd, but rnd was nowhere before. Plus pop is a string. You need to cast pop to a numeric type.
In general, you should refactor this thing:

dict = {1: trys - 1}
tr1 = dict[1]

the for i in range loop can also be run in the reverse order, from trys to 0 =)
Also here: your trys will always be the same.
if trys == 0:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question