write a code in python
Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries.
#!/usr/bin/python3
#(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are less than 6. If they are, the function will return true if they aren’t, the function will return false.
def all_less_than_6(x):
pass
# This function will ask the user for a string. It will check to see if the inputted string contains only digits and all the digits are less than 6. If they aren’t ask the input to try again and keep asking until they get correct
def get_input():
pass
# (1 Mark) This function will take in a num between 0 and 9 it should return
# C if x is 0
# E if x is 1
# L if x is 2
# R if x is 3
# N if x is 4
# O if x is 5
# Y if x is 6
# A if x is 7
# D if x is 8
# W if x is 9
def conv(x):
pass
# This function will do some weird math on a string of digits. It should return a string that numi = input_stri + input_stri+1,if i = len(input_str)-1 then numi = input_stri + input_str0. If numi is equal to 10 then set it to be 0. Example 1243 will be 3674 because 1+2 = 3, 2+4 = 6, 4+3 = 7 and 3+1 = 4. Another example 55 will be 00 because 5+5 = 10 and 5+5 = 10
def weirdness(input_str):
pass
# This function will print out a message using a string of digits and the conv function.
def more_weirdness(x):
#This is where you make it all work
if __name__ == "__main__" :
print(“HELLO WORLD”)
When your script is done it should be able to recreate the following output
Enter in a string of digits where each digit is less than 6
>43326
Try again! Enter in a string of digits where each digit is less than 6
>1423H34
Try again! Enter in a string of digits where each digit is less than 6
>1243
3674
RYAN
Add your scripts output for the following input 4422102505543
In case of any query do comment. Thanks
Code:
#!/usr/bin/python3
#(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are less than 6. If they are, the function will return true if they aren’t, the function will return false.
def all_less_than_6(x):
for digit in x:
digit = int(digit)
if digit >=6:
return False
return True
#(2 Marks) This function will ask the user for a string. It will check to see if the inputted string contains only digits and all the digits are less than 6. If they aren’t ask the input to try again and keep asking until they get correct
def get_input():
while True:
stringOfDigits = input("Enter in a string of digits where each digit is less than 6\n")
if stringOfDigits.isdigit() and all_less_than_6(stringOfDigits):
return stringOfDigits
else:
print("Try again!",end=" ")
# (1 Mark) This function will take in a num between 0 and 9 it should return
# C if x is 0
# E if x is 1
# L if x is 2
# R if x is 3
# N if x is 4
# O if x is 5
# Y if x is 6
# A if x is 7
# D if x is 8
# W if x is 9
def conv(x):
#create a list of return values and it's index would be the number and item would be the return value
# as we put C at 0 index, E at index 1 , L at index 2 and ..... W on index 9 and simply return the value at the given index
convertList = ['C','E','L','R','N','O','Y','A','D','W']
return convertList[x]
#(4 Marks) This function will do some weird math on a string of digits. It should return a string that numi = input_stri + input_stri+1,if i = len(input_str)-1 then numi = input_stri + input_str0. If numi is equal to 10 then set it to be 0.
#Example 1243 will be 3674 because 1+2 = 3, 2+4 = 6, 4+3 = 7 and 3+1 = 4. Another example 55 will be 00 because 5+5 = 10 and 5+5 = 10
def weirdness(input_str):
#store the length of the input_str
length = len(input_str)
#initialize return value with empty string
sumOfDigits =""
for i in range(length):
#take out the first digit
firstDigit = int(input_str[i])
#check for length and according take out next digit or first Digit
if i == length -1:
secondDigit = int (input_str[0])
else:
secondDigit = int (input_str[i+1])
#do a local sum
localSum = firstDigit + secondDigit
#if localSum is 10 then make it 0
if localSum == 10:
localSum =0
#add localSum to the return value
sumOfDigits += str(localSum)
return sumOfDigits
#(1 Marks) This function will print out a message using a string of digits and the conv function.
def more_weirdness(x):
weirdString = weirdness(x)
convResult =""
for digit in weirdString:
convResult += conv(int(digit))
print(weirdString)
print(convResult)
#This is where you make it all work
if __name__ == "__main__" :
digits = get_input()
more_weirdness(digits)
========Screen shot of the code for indentation========
output:
Get Answers For Free
Most questions answered within 1 hours.