# Go over each character of input
# If that single character is NOT
# a valid digit of that base,
# return False immediately
# If you finish going through ALL the characters,
# then return True
def is_base_x (num_string, base):
# YOUR CODE STARTS HERE
# YOUR CODE ENDS HERE
# This will involve looping through each
# character in num_string, converting it
# to its integer equivalent, multiplying
# it by the base raised to the current
# exponent, and adding it to a running sum
def base_x_value (num_string, base):
# YOUR CODE STARTS HERE
# YOUR CODE ENDS HERE
"""
Read the following code so that you understand
what it is doing. However, you do not need to (and
should not) make any changes to it.
"""
base = int (input ("Specify your base (2-10 or 16): "))
print()
if not (2 <= base <= 10 or base == 16):
print (base, "is not a valid base for this program.")
print ("Setting base to a default of 10.")
print()
base = 10
num_string = input ("Enter a valid base-" + str(base) + " number up to 8 digits long: ")
num_string = num_string[0:8] # In case user goes over 8 digits
print()
if is_base_x (num_string, base):
print ("You entered:", num_string)
print ("The base-10 value of", num_string, "is", base_x_value(num_string, base))
else:
print (num_string, "is not a valid", "base-" + str(base), "number!")
print ("Please re-run the program and try again...")
The program will prompt the user for some input -- specfically, a base (2-10 or 16) and an 8-digit number expressed in that base. If the number is a valid number in that base, the program will calculate its base-10 equivalent. Remember that base-16 includes all the base-10 digits, plus A-F to represent the values 10-15; ideally, your program would be able to handle both uppercase and lowercase versions of those. Otherwise, it will print an error message.
You will need to do the following things:
- Write a method to verify whether or not the user's input string is a valid number in the specified base.
- Write a method to calculate the value of the user's input string, in base-10.
Note that because of user input, the output will look different each time you run the program. When running the program, you can use this site to check the correctness of your program's solution.
Program output examples:
Example #1
Specify your base (2-10 or 16): 2
Enter a valid base-2 number up to 8 digits long: 10111010
You entered: 10111010
The base-10 value of 10111010 is 186
Example #2
Specify your base (2-10 or 16): 7
Enter a valid base-7 number up to 8 digits long: 20132601
You entered: 20132601
The base-10 value of 20132601 is 1672077
Example #3
Specify your base (2-10 or 16): 4
Enter a valid base-4 number up to 8 digits long: 10170013
10170013 is not a valid base-4 number!
Please re-run the program and try again...
Example #4
Specify your base (2-10 or 16): 16
Enter a valid base-16 number up to 8 digits long: 1a13f2
You entered: 1a13f2
The base-10 value of 1a13f2 is 1709042
Question: How did the process of editing this program go for you? What were your main challenges and how did you overcome them? What did you learn that may be of use as you move along in this class?