I have been going through the harvard cs50 problems, and just did the convert to camel case problem. I did go a step beyond and made it convert from snake to camel and camel to snake based on what what inputed. Can anyone review it for me and see if there is a better way to do anything?
#get variable
def main():
variable = input('What variable do you want to convert?')
type = camel_case_check(variable)
if type == 'nothing':
print('The variable does not need converting')
elif type == 'camel':
print('The variable is converting to snake case')
print(convert_to_snake(variable))
elif type == 'spaces':
print('The variable has spaces, please remove the spaces and try again')
else:
print('The variable is converting to camel case')
print(convert_to_camel(variable))
#check if its camel case or snake case
def camel_case_check(variable):
if variable.count(' '):
return 'spaces'
if variable.count('_') == 0:
for letter in variable:
if letter.isupper():
return 'camel'
return 'nothing'
return 'snake'
#convert to opposite case
def convert_to_snake(variable):
new_variable = ''
for letter in variable:
if letter.islower():
new_variable = new_variable + letter
else:
letter = letter.lower()
new_variable = new_variable + '_' + letter
return new_variable
def convert_to_camel(variable):
new_variable = ''
underscore_count = 0
for letter in variable:
if letter == '_':
underscore_count +=1
elif underscore_count == 1:
new_variable = new_variable + letter.upper()
underscore_count = 0
else:
new_variable = new_variable + letter
return new_variable
if __name__ == '__main__':
main()