L
L
lisvockah2021-06-30 16:40:44
Python
lisvockah, 2021-06-30 16:40:44

I have a problem with python lists and input, why doesn't it work?

When the user enters any numbers that start with 0 (for example, 0999 )
Zero is not taken into account in the list (enter ['9','9','9'])

import random
x=0
b=0
help = ['4','4','4','4']
kloc = ['0','1','2','3','4','5','6','7','8','9']
random.shuffle(kloc)
kloc[random.randint(0,3)]
for l in range(0, 6):
  kloc.pop(x+1)
#print(kloc)
#сокрощене плроля до 4 значень 
'''блок слздане пароля'''


while True:
    cow = 0 
    bull = 0
    try:
      user_input  = int(input())
    except ValueError:
      print("не верний вод")
      continue
  # проверка на int 
    clok_user_input0 = str(user_input)
    clok_user_input= list(clok_user_input0)
    #привращене в список 
    print(clok_user_input) 
    if len(clok_user_input) != 4: 
      print("цифор не є 4") 
      continue
    # проверка на длину пароля 
    
    
    if clok_user_input == kloc:
      print("виграш")
      
    if clok_user_input[0] == kloc[0]:
      cow+=1
      
    if clok_user_input[1] == kloc[1]:
      cow+=1
      
    if clok_user_input[2] == kloc[2]:
      cow+=1
      
    if  clok_user_input[3] == kloc[3]:
      cow+=1
    if b == 1:
      print("тільки 1 ")
    elif clok_user_input == help: 
      try:
        help_input  = int(input())
      except ValueError:
        print("не верний вод")
        continue
        
      print(kloc[help_input])
      b+=1
    # проверка на попадене 
    bull0 = list(set(clok_user_input) & set(kloc))
    # проверка на пересичене
    bull = len(bull0)
    print("кількість коров:", cow, 'кількість биків', bull) 
    
  <img src="https://habrastorage.org/webt/60/dc/73/60dc73da9a746783726596.jpeg" alt="image"/><img src="https://habrastorage.org/webt/60/dc/73/60dc73f1baa73080200401.jpeg" alt="image"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-06-30
@lisvockah

Because numbers don't start at 0. There are better checks like the built-in isdigit() method.

try:
      user_input  = int(input())
    except ValueError:
      print("не верний вод")
      continue

replaced by
user_input  = input()
if not user_input.isdigit():
    continue

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question