In this collection of challenges you are going to manipulate numbers and perform basic arithmetic operations with numbers as well as use string manipulation techniques to format numbers.
Challenge #1: Square Number
number = int(input("Type a number:"))
squareNumber = number * number
print("The Square Number is " + str(squareNumber))
Converting a user input to an integer is required to do some maths based calculations such as the calculation used on line 2.
Vice versa, we can convert an integer to a string using the str() function on line 3.
Challenge #2: Total 100
number = int(input("Type a number:"))
total100 = 100 - number
print("The Total 100 Number is " + str(total100))
Challenge #3: Odd or Even?
number = int(input("Type a number:"))
if (number % 2) == 0:
print("This number is even.")
else:
print("This number is odd.")
Challenge #4: Telephone Number
number = input("Type your telephone number usin 11 digits:")
telephoneNumber = "(" + number[0:5] + ") " + number[5:8] + " " + number[8:11]
print(telephoneNumber)
Notice that in this challenge we are treating the user input as a string not a number. (We did not convert the user input on line 1 to an integer as we did in previous challenges using the int() function.) Each digit of our number/string is in fact a character from the string.
Challenge #5: Palindrome
number = input("Type a large number:")
palindrome = ""
for digit in number:
palindrome = digit + palindrome
palindrome = number + palindrome
print("The palindrome number is " + palindrome)
Notice that in this challenge, once again we are treating the user input as a string not a number. (We did not convert the user input on line 1 to an integer as we did in previous challenge using the int() function.) Each digit of our number/string is in fact a character from the string.
Challenge #6: Number Upgrade
number = input("Type a large number:")
numberUpgrade = ""
for digit in number:
if int(digit)==9:
newDigit = 0
else:
newDigit = int(digit) + 1
numberUpgrade = numberUpgrade + str(newDigit)
print("The Number Upgrade is " + numberUpgrade)
We retrieve each digit (character) one at a time and have to convert these digits to integers using the int() on line 5 and 8 to be able to perform maths calculation with them (adding them up).
Challenge #7: Spaced Out Number
number = input("Type a large number:")
spacedOutNumber=""
for digit in number:
spacedOutNumber = spacedOutNumber + "-" + str(digit)
print("The Spaced Out Number is " + spacedOutNumber[1:])
Notice the slicing approach used to truncate the string from its first digit on line 7 using the string[1:] which returns all the characters from a string from position 1. (So slicing out the first character which is at position 0).
Challenge #8: Total Number
number = input("Type a large number:")
totalNumber = 0
for digit in number:
totalNumber += int(digit)
print("The Total Number is " + str(totalNumber))
Challenge #9: Plus Minus
number = input("Type a large number:")
plusMinusNumber = 0
plusminus = 1
for digit in number:
plusMinusNumber += int(digit) * plusminus
plusminus = plusminus * (-1)
print("The Plus Minus is " + str(plusMinusNumber))
Challenge #10: Digit Grouping
Ask the user to enter any large number (e.g. at least 3 digits long). The program should display the result of formatting this number using the comma. For instance if the user enters 65738924 the program should return 65,738,924.
number = input("Type a large number:")
digitGroupingNumber = ""
counter=0
for digit in number[::-1]:
if counter==3:
digitGroupingNumber = digit + "," + digitGroupingNumber
counter=0
else:
digitGroupingNumber = digit + digitGroupingNumber
counter+=1
print("The Digit Grouping Number is " + digitGroupingNumber)






