Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- by Curtis Michels for cs160 @ COCC
- v0.001 date: 11/30/2017
- """
- import math
- #checks if user input is an integer, will continue asking until they enter one
- def getinput():
- while True:
- try:
- out = int(input())
- except ValueError:
- print("Please only enter integers")
- continue
- else:
- return out
- def convertDecToBase(num,base):
- #takes a number from base 10 and converts it to the specified base, prints & returns the result
- i = 0
- num = int(num)
- base = int(base)
- #figuring out how many places in the new base the number needs to take up
- while True:
- if (num / base**i < 1):
- if (i == 1):
- print(chr(num+55))
- return(0)
- highPow=i-1
- #print('The highest power of base', base, 'that goes into',num,'is', highPow)
- break
- i = i+1
- x = ''
- #loop is written in such a way that it counts down instead of up so the digits are in the proper order
- i = highPow
- while i >= 0:
- #replaces the digit with a corresponding letter if it's >= 10
- if (num//base**i >= 10):
- x = x+chr(num//base**i+55)
- #otherwise just put the regular number in
- else:
- x = x+str(num//base**i)
- num = num%base**i
- i = i-1
- #this is just to handle the situation where the number is less than the base being converted to
- if(num != 0):
- x = x[:-1]
- print(x+chr(num+55))
- return(x+chr(num+55))
- else:
- print(x)
- return(x)
- def main():
- print('Please input a decimal number ')
- x = getinput()
- print('Please input a base to convert the number to ')
- y = getinput()
- convertDecToBase(x,y)
- main()
- #runs again if the user wants it to
- while True:
- x = input('Convert another number? Y/N ')
- if (x == 'Y' or x == 'y'):
- main()
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement