Hello! Correctly translated the pascal to Python code, but it gives an error. key error

Python


print("Введите число ") n=int(input()) k=n // 100 n=n % 100 d={9: "девятьсот", 8: "восемьсот", 7: "семьсот" , 6:" шестьсот", 5: "пятьсот", 4: "четыреста", 3: "триста", 2: "двести", 1: "сто" } print(d[k]) k=n//10 

The question is resolved and does not require an answer .

Closed due to the fact that off-topic participants Dmitriy Simushev , Kromster , rjhdby , Vadizar , ermak0ff 5 Mar '17 at 8:36 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Dmitriy Simushev, Kromster, Vadizar
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • ten is not enough, but in general they would log everything or, under debugging, they would launch and find out at what number the error occurred - gil9red

1 answer 1

Tested working

 #!/usr/bin/env python3 #-*- coding: utf-8 -*- n=int(input("Введите число ")) r1=n // 100 # формируем первый разряд if r1>0 : d1={9:"девятьсот", 8:"восемьсот", 7:"семьсот" , 6:" шестьсот", 5:"пятьсот", 4:"четыреста", 3:"триста", 2:"двести", 1:"сто"} print(d1[r1], end=' ') r2=n%100//10 # формируем второй разряд if (r2>1): d2={9:"девяносто" , 8:"восемьдесят" , 7:"семьдесят" , 6:"шестьдесят" , 5:"пятьдесят" , 4:"сорок" , 3:"тридцать" , 2:"двадцать" } print(d2[r2], end=' ') if r2==1: r3=n%100 # формируем третий разряд else: r3=n%10 if r3 in range(1,20): d3={19:"девятнадцать" , 18:"восемнадцать", 17:"семнадцать", 16:"шестнадцать" , 15:"пятнадцать", 14:"четырнадцать", 13:"тринадцать", 12:"двенадцать", 11:"одиннадцать", 10:"десять", 9:"девять", 8:"восемь", 7:"семь", 6:"шесть", 5:"пять", 4:"четыре", 3:"три" , 2:"двa", 1:"один" } print (d3[r3]) 
  • Unfortunately, it does not work for me - prntscr.com/ef5h7h - Sergey
  • Correct this dictionary: d1 = {9: "nine hundred", 8: "eight hundred", 7: "seven hundred", 6: "six hundred", 5: "five hundred", 4: "four hundred", 3: "three hundred", 2 : "two hundred", 1: "one hundred", 0: ""} And this piece if (r2> 1 or r2 == 0): d2 = {9: "ninety", 8: "eighty", 7: "seventy ", 6:" sixty ", 5:" fifty ", 4:" forty ", 3:" thirty ", 2:" twenty ", 0:" "} print (d2 [r2]) - Kirill Malyshev
  • @ Kirill Malyshev, you just have a two-digit number, works with only three significant ones, I honestly did not suggest such a probability, now I’ll correct the code. - Artem
  • @ Kirill Malyshev Gotvo, corrected - Artem