Python program to find a lucky number



Lucky Number: Write a python program that asks a user to enter a 5-digit number to check whether he has won the lottery. The program must display the lucky number digit by digit and declares the 5-digit lucky number at the end. If the numbers matches give a remark “Congratulations! You are a winner”. Otherwise, say “Sorry! Try your luck next time”.

This program will give you a good practice of understanding:


Code:

import random

my_lucky_num = input('Enter 5 digit number to try your luck: ')
length_num = len(my_lucky_num)
if length_num == 5:
    list = []
    for i in range(5):
        list.append(random.randrange(1,10))
    for i in range(len(list)):
        draw_seq = int(i) + 1
        print( 'Draw ',draw_seq,": ", end=" ")
        print(list[i])
    x = ''
    for each in list:
        x += str(each)
        draw_num = int(x)
    lucky_num = int(my_lucky_num)
    print('Draw Number: ', draw_num)
    print('Your Number: ', lucky_num)
    if draw_num == lucky_num:
        print('Congratulations! You are a winner.')
    else:
        print('Sorry! Try your luck next time.')
else:
    print('Invalid number. Please try with 5 digits.')

Output:

Enter 5 digit number to try your luck: 56733
Draw  1 :  5
Draw  2 :  6
Draw  3 :  8
Draw  4 :  6
Draw  5 :  9
Draw Number:  56869
Your Number:  56733
Sorry! Try your luck next time.

>>>
Enter 5 digit number to try your luck: 26743895
Invalid number. Please try with 5 digits.