Advertisement
Adehumble

Smart Number Base Calculator

Mar 2nd, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. print("\t\tSMART NUMBER BASE CALCULATOR")
  2. print("\n")
  3. print("What do u want to do?\nA) I want to convert from base 10 to other number base\nB) I want to convert from any number base to base 10")
  4. print("\n")
  5.  
  6. #This will accepts a user choice
  7. users_choice=input("Indicate your choice above by selecting option 'A' or 'B':  ").upper()
  8. print("\n")
  9.  
  10. #The following lines will convert from base 10 to any number base. One interesting thing is that, it'll keep prompting the user for the correct format or data type of all expected inputs
  11. if  users_choice=="A":
  12.     print("\tDear user, just to remind you, i'll be helping you to convert from base 10 to any other number base of your choice. LET'S GET STARTED!")
  13.     print("\n")
  14.     while True:
  15.         try:
  16.             users_num=abs(int(float(input('Enter the number in base 10: '))))
  17.             break
  18.         except ValueError:
  19.             print("That is a wrong input! I am expecting a positive integer. Please try again.")
  20.             print("\n")
  21.     while True:
  22.         try:
  23.             base=abs(int(float(input("Enter the number base you would like to convert to: "))))
  24.             break
  25.         except ValueError:
  26.             print("That is a wrong input! I am expecting a positive integer. Please try again.")
  27.             print("\n")
  28. #Now, we have come to the most technical part...smiles.
  29.     rem_list=[]
  30.     answer=""
  31.     main=users_num
  32.     while main>=base:
  33.         rem=main%base
  34.         rem_list.append(rem)
  35.         main=main//base
  36.     if main<base:
  37.         rem_list.append(main)
  38.     for r in rem_list[::-1]:
  39.         answer=answer+str(r)
  40.     print(users_num,"in base 10 is:",answer, "in base",base)
  41.    
  42.  
  43. #The following lines will convert from any number base to base 10. One interesting thing is that, it'll keep prompting the user for the correct format or data type of all expected inputs
  44. elif users_choice=="B":
  45.     print("\tDear user, just to remind you, i'll be helping you to convert from any number base of your choice to base 10. LET'S GET STARTED!")
  46.     print("\n")
  47.     answer=0
  48.     while True:
  49.         try:
  50.             base=abs(int(float(input("What number base do you wish to convert to base 10? "))))
  51.             break
  52.         except ValueError:
  53.             print("That is a wrong input! I am expecting a positive integer. Please try again.")
  54.             print("\n")
  55.     while True:
  56.         try:
  57.             users_num=abs(int(float(input("Enter the number: "))))
  58.             break
  59.         except ValueError:
  60.             print("That is a wrong input! I am expecting a positive integer. Please try again.")
  61.             print("\n")
  62. #Many things are going down here too
  63.     num_list= [int(n) for n in str(users_num)]
  64.     p=len(num_list)
  65.     for q in num_list:
  66.         p=p-1
  67.         answer=answer + (q* base**p)
  68.         if p<0:
  69.             break
  70.     print(users_num,"in base", base,"is:", answer, "in base 10")
  71.  
  72. else:
  73.     print("You entered an incorrect option. Your choices are only limited to A or B. Please, try again!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement