Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python: Write a function getInt(question) that asks the user for a number. If the input is not an int, ask again. If the user enters
Python:
Write a function getInt(question) that asks the user for a number. If the input is not an int, ask again. If the user enters exit, the program will call sys.exit() to exit immediately. Use the question given as the output to the user.
Write a function binaryStr(num,bits) that converts the the number num to a binary string with bits bits. If the number is to big to fit in the bits given, truncate it. Make a string using the "1" and "0" characters.
As we saw in lecture, each number is represented by a binary value. The number of bits in the binary number determines the maximum value that can be stored For example, an 8 bit number can store from 0 to 255 We can convert a number to Binary through repeated division Take 254 as an example 254 % 2-0 254//2 127 127 % 2-1 127 //2-63 63 % 2-1 63//2-31 31 %2-1 31 //2 15 15%2-1 15//2 7 7%2-1 7//2 3 3%2-1 3//2-1 1%2:1 1//2 0 When the quotient is equal to 0, we have no more values. The binary value is the remainders read from bottom to topStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started