Advertisement
Guest User

For x in i Range(0, stop)

a guest
Nov 30th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. __author__ = 'Zoap'
  2.  
  3. def Search():
  4.  
  5.     inv = "\nInvalid input"
  6.     invx = "\nNumber to search for must be a number!"
  7.     invs = "\nLength must be a number!"
  8.  
  9.     print("\nThis program allows you to search for the number of possible combinations of a pin number of length x, "
  10.           "given that you know one of the digits")
  11.  
  12.     x = str(input("\nInput number to search for: "))
  13.  
  14.     if x.isdigit() is False:
  15.         print(invx)
  16.         Search()
  17.         return
  18.     elif x.isdigit() is True:
  19.         def PinLength():
  20.  
  21.             s = str(input("\nInput length of pin code: "))
  22.  
  23.             if s.isdigit() is False:
  24.                 print(invs)
  25.                 PinLength()
  26.                 return
  27.  
  28.             elif int(s) > 7:
  29.                 print("\nLength can not be more than 7 digits!")
  30.                 PinLength()
  31.                 return
  32.  
  33.             import math
  34.  
  35.             stop = int((math.pow(10, int(s))))
  36.  
  37.             if stop < int(x):
  38.                 print("\nLength of pin makes the number to search for bigger than the possible number of variations!")
  39.                 Search()
  40.                 return
  41.  
  42.             elif s.isdigit() is True:
  43.                 def CalcForLoop():
  44.                     number_list = []
  45.  
  46.                     # For every integer in range add int to list and convert to string
  47.                     for i in range(0, (stop + 1)):
  48.                         number_list.append(str(i))
  49.  
  50.                     # For every str in list containing x, count x
  51.                     x_numbers = [a for a in number_list if x in a]
  52.  
  53.                     # Percent of total numbers in range containing digit x
  54.                     percent = len(x_numbers) / (stop / 100)
  55.  
  56.                     if int(x) >= 10:
  57.                         print("\nThere are a total of " + str(len(x_numbers)) +
  58.                               " numbers between 0 and " + str(stop) + " containing the number " + x)
  59.                         print("\nThat is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " +
  60.                               str(stop))
  61.  
  62.                     elif int(len(x_numbers)) == 1:
  63.                         print("\nThere are a total of " + str(len(x_numbers)) +
  64.                               " number between 0 and " + str(stop) + " containing the digit " + x)
  65.                         print("\nThat is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " +
  66.                               str(stop))
  67.  
  68.                     else:
  69.                         print("\nThere are a total of " + str(len(x_numbers)) +
  70.                               " numbers between 0 and " + str(stop) + " containing the digit " + x)
  71.                         print("\nThat is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " +
  72.                               str(stop))
  73.  
  74.                     def RunAgain():
  75.  
  76.                         yes = "y" or "Y"
  77.                         no = "n" or "N"
  78.  
  79.                         choice = input("\nRun again? y/n: ")
  80.                         if choice is yes:
  81.                             Search()
  82.                             return
  83.  
  84.                         elif choice is no:
  85.                             exit()
  86.  
  87.                         else:
  88.                             print(inv)
  89.                             RunAgain()
  90.                             return
  91.  
  92.                     RunAgain()
  93.  
  94.                 CalcForLoop()
  95.  
  96.         PinLength()
  97.  
  98. Search()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement