Converting decimal numbers to binary with functions in python - python

I have to create a program to convert decimal numbers to binary for my class but im lost. These are the directions.
3 Files must be used
user_input.py
Obtain an integer from the user between 0-255.
No conversion or print statements allowed in the user_input.py
Obtaining the integer must be done in a function
No import statements allowed
conversion.py
Convert the number provided to a binary number (e.g., 254 provided and converted to 11111110)
No print statements, input statements, or built in functions to convert a number to binary. Conversion must be done manually.
Small hint, concatenating the 1s and 0s is going to be tough b/c python will want to add the numbers (e.g., 1+1 = 2 vs 1+1 = 11). So, how are you going to handle this issue?
The conversion must be done in a function
No import statements allowed
main.py
Print the eight bit binary representation (00000000 - 11111111) of the number provided and converted. To do so, you will have to access the necessary functions in conversion.py and user_input.py
Note - the program is only allowed to access functions. No direct access to variables allowed.
No input or conversions allowed in main.py
Note - the binary print must only be the eight binary numbers. No commas, brackets etc. - ['1', '0', '0', ...] or [1,1,0,0,1..] are not allowed.
to make the function to get the inputted number I use
def number ():
num =int(input('Pick a number from 0 - 255'))
return num
number()
After that when I do not know how I would access it without importing it.
This is the code I'm using to convert
def conversion(num):
cnv_bin = ''
num = ''
while num // 2 > 0:
if num % 2 == 0:
cnv_bin += '0'
else:
cnv_bin += '1'
num = num // 2
return cnv_bin
In the last file I tried
import conversion
import user_input
print(conversion.conversion(user_input.number()))
and that gave me this error
Traceback (most recent call last):
File "C:/Users/adkir/PycharmProjects/lab_23/main.py", line 4, in <module>
print(conversion.conversion(user_input.number()))
File "C:\Users\adkir\PycharmProjects\lab_23\conversion.py", line 5, in
conversion
while num // 2 > 0:
TypeError: unsupported operand type(s) for //: 'str' and 'int'
So basically im lost. Can anyone point me in the right direction so I can figure this out?

Your conversion function takes num as a parameter and yet it overwrites num's value by assigning an empty string to it.
Remove the line:
num = ''
and it should work.

Related

Unsupported Operand Error generated from custom factorial function

f = input("Enter the number to be squared : ")
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
print(factorial(f))
I am trying to get the factorial of the input number
When running the code I receive the following error:
TypeError: unsupported operand type(s) for -: 'str' and 'int'
You have to typecast your input into float or integer for it to be accepted on your mathematical equation. You can even place that in a while loop together with the try: and except ValueError: to cover the error in entry or give messages when your user does not satisfy the type you want your f variable. You can read the documentation for more info.
f = int(input("Enter the number to be squared : "))
OR
f = float(input("Enter the number to be squared : "))
On your original code f would take any a string value even if the user inputs the number 2 for example. So when you call the function that solves your factorial, it will return an error, because of course, you cannot do factorial notation on a string.
You forgot closing parenthesis ')' at the parenthesis end.
The data type of variable 'f' must be an integer and you are passing a string. As 'input' assigns string data type by default to the variable.
Make the following changes:
f = int(input("Enter the number to be squared : "))
print(factorial(f))
to lines 1 and the last line respectively.
The first problem that I see is with your data types. You will need to convert f to either an integer or a float so that it can be used in the mathematical operations in your function. You may also want to include some exception handling in case the user enters something other than a number.
You can convert the type by using int(f) or float(f)
As another commenter mentioned, you are also missing a parenthesis at the end of print(factorial(f).
If you make these changes your code should run.

I am trying to execute Kaprekars Constant in python, but I am getting this following error

def difficultNumber(num):
count=0
while num !=6174:
num = list(str(num))
if len(num) < 4:
while len(num) != 4:
num.insert(1,'0')
count +=1
des = int(''.join(sorted(str(num), reverse=True)))
asc = int(''.join(sorted(str(num))))
result = int(des) - int(asc)
return count
print(difficultNumber(input()))
===========================
8786
Traceback (most recent call last):
File "cb_ex9.py", line 19, in <module>
print(difficultNumber(input()))
File "cb_ex9.py", line 12, in difficultNumber
des = int(''.join(sorted(str(num), reverse=True)))
ValueError: invalid literal for int() with base 10: "][8876,,,'''''''' "
You're getting the error because you're using the variable num to represent two things: either a number, or a list of 1-element strings containing the digits that make up the number, and are having difficulty keeping track of which of these two things num is all the way through your program.
The body of your outer while loop starts by converting num as a number to num as a list of digit strings, and then makes the number up to four digits by inserting zero digits at the front. So far so good. You then compute des and asc using the following code
des = int(''.join(sorted(str(num), reverse=True)))
asc = int(''.join(sorted(str(num))))
It seems you have forgotten num is now a list of strings. When you call str on num, you are calling str on the list ['8', '7', '8', '6'], and hence you end up with the string "['8', '7', '8', '6']". Sorting the characters of that string in reverse order gives you the string "][8876,,,'''''''' ", which of course cannot be converted to a number, hence the error you get.
So, let's remove the call to str from the calculations of des and asc:
des = int(''.join(sorted(num, reverse=True)))
asc = int(''.join(sorted(num)))
What happens when we run your program now? We get the same error!
At this point we need to be careful not to simply dismiss this change as having had no effect. It has; it just doesn't look like it has. We can prove that it has had an effect by printing the value of the count variable after incrementing it: before, it only printed 1, now it prints 1 and 2. Now we are getting into the second iteration of the while loop; before we were failing in the first.
But why are we still getting the same error?
At the start of the while loop, we call num = list(str(num)). Assuming num is an int, this converts num to a list of digits. However, at the end of the while loop, num isn't converted back to an int. It's still a list of digits. The next time through the while loop we end up calling str on a list again. But why wasn't num converted back to an int at the end of the while loop? Because the difference between des and asc was assigned to result, but nothing is done with the value in result. You've performed a calculation and then ignored the result of it.
I'm guessing that instead of writing
result = int(des) - int(asc)
you meant to write
num = int(des) - int(asc)
Making this change fixes your code and it appears to work as you would expect. Enter 8786 and it prints 3.
I would recommend to you that you go back through your code and introduce a second variable, named numDigits or digitsOfNum for example, and use this instead of num when you are working with a list of digits. That way your code should hopefully be easier for you to understand. You might also want to think about what happens if someone enters a number longer than four digits (e.g. 123456), or contains the same digit repeated four times, e.g. 5555.

Print function input into int [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
My goal is very simple, which makes it all the more irritating that I'm repeatedly failing:
I wish to turn an input integer into a string made up of all numbers within the input range, so if the input is 3, the code would be:
print(*range(1, 3+1), sep="")
which obviously works, however when using an n = input() , no matter where I put the str(), I get the same error:
"Can't convert 'int' object to str implicitly"
I feel sorry to waste your collective time on such an annoyingly trivial task..
My code:
n= input()
print(*range(1, n+1), sep="")
I've also tried list comprehensions (my ultimate goal is to have this all on one line):
[print(*range(1,n+1),sep="") | n = input() ]
I know this is not proper syntax, how on earth am I supposed to word this properly?
This didn't help, ditto this, ditto this, I give up --> ask S.O.
I see no reason why you would use str here, you should use int; the value returned from input is of type str and you need to transform it.
A one-liner could look like this:
print(*range(1, int(input()) + 1), sep=' ')
Where input is wrapped in int to transform the str returned to an int and supply it as an argument to range.
As an addendum, your error here is caused by n + 1 in your range call where n is still an str; Python won't implicitly transform the value held by n to an int and perform the operation; it'll complain:
n = '1'
n + 1
TypeErrorTraceback (most recent call last)
<ipython-input-117-a5b1a168a772> in <module>()
----> 1 n + 1
TypeError: Can't convert 'int' object to str implicitly
That's why you need to be explicit and wrap it in int(). Additionally, take note that the one liner will fail with input that can't be transformed to an int, you need to wrap it in a try-except statement to handle that if needed.
In your code, you should just be able to do:
n = int(input())
print(*range(1,n+1),sep="")
But you would also want to have some error checking to ensure that a number is actually entered into the prompt.
A one-liner that works:
print(*range(1, int(input()) + 1), sep="")

ValueError: invalid literal for int () with base 10 [duplicate]

This question already has answers here:
ValueError: invalid literal for int() with base 10: ''
(15 answers)
Closed last month.
I wrote a program to solve y = a^x and then project it on a graph. The problem is that whenever a < 1 I get the error:
ValueError: invalid literal for int () with base 10.
Any suggestions?
Here's the traceback:
Traceback (most recent call last):
File "C:\Users\kasutaja\Desktop\EksponentfunktsioonTEST - koopia.py", line 13, in <module>
if int(a) < 0:
ValueError: invalid literal for int() with base 10: '0.3'
The problem arises every time I put a number that is smaller than one, but larger than 0. For this example it was 0.3 .
This is my code:
# y = a^x
import time
import math
import sys
import os
import subprocess
import matplotlib.pyplot as plt
print ("y = a^x")
print ("")
a = input ("Enter 'a' ")
print ("")
if int(a) < 0:
print ("'a' is negative, no solution")
elif int(a) == 1:
print ("'a' is equal with 1, no solution")
else:
fig = plt.figure ()
x = [-2,-1.75,-1.5,-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25,1.5,1.75,2]
y = [int(a)**(-2),int(a)**(-1.75),int(a)**(-1.5),int(a)**(-1.25),
int(a)**(-1),int(a)**(-0.75),int(a)**(-0.5),int(a)**(-0.25),
int(a)**(0),int(a)**(0.25),int(a)**(0.5),int(a)**(0.75),
int(a)**1,int(a)**(1.25),int(a)**(1.5),int(a)**(1.75), int(a)**(2)]
ax = fig.add_subplot(1,1,1)
ax.set_title('y = a**x')
ax.plot(x,y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.savefig("graph.png")
subprocess.Popen('explorer "C:\\Users\\kasutaja\\desktop\\graph.png"')
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
if __name__ == "__main__":
answer = input("Restart program? ")
if answer.strip() in "YES yes Yes y Y".split():
restart_program()
else:
os.remove("C:\\Users\\kasutaja\\desktop\\graph.png")
Answer:
Your traceback is telling you that int() takes integers, you are trying to give a decimal, so you need to use float():
a = float(a)
This should work as expected:
>>> int(input("Type a number: "))
Type a number: 0.3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0.3'
>>> float(input("Type a number: "))
Type a number: 0.3
0.3
Computers store numbers in a variety of different ways. Python has two main ones. Integers, which store whole numbers (ℤ), and floating point numbers, which store real numbers (ℝ). You need to use the right one based on what you require.
(As a note, Python is pretty good at abstracting this away from you, most other language also have double precision floating point numbers, for instance, but you don't need to worry about that. Since 3.0, Python will also automatically convert integers to floats if you divide them, so it's actually very easy to work with.)
Previous guess at answer before we had the traceback:
Your problem is that whatever you are typing is can't be converted into a number. This could be caused by a lot of things, for example:
>>> int(input("Type a number: "))
Type a number: -1
-1
>>> int(input("Type a number: "))
Type a number: - 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '- 1'
Adding a space between the - and 1 will cause the string not to be parsed correctly into a number. This is, of course, just an example, and you will have to tell us what input you are giving for us to be able to say for sure what the issue is.
Advice on code style:
y = [int(a)**(-2),int(a)**(-1.75),int(a)**(-1.5),int(a)**(-1.25),
int(a)**(-1),int(a)**(-0.75),int(a)**(-0.5),int(a)**(-0.25),
int(a)**(0),int(a)**(0.25),int(a)**(0.5),int(a)**(0.75),
int(a)**1,int(a)**(1.25),int(a)**(1.5),int(a)**(1.75), int(a)**(2)]
This is an example of a really bad coding habit. Where you are copying something again and again something is wrong. Firstly, you use int(a) a ton of times, wherever you do this, you should instead assign the value to a variable, and use that instead, avoiding typing (and forcing the computer to calculate) the value again and again:
a = int(a)
In this example I assign the value back to a, overwriting the old value with the new one we want to use.
y = [a**i for i in x]
This code produces the same result as the monster above, without the masses of writing out the same thing again and again. It's a simple list comprehension. This also means that if you edit x, you don't need to do anything to y, it will naturally update to suit.
Also note that PEP-8, the Python style guide, suggests strongly that you don't leave spaces between an identifier and the brackets when making a function call.
As Lattyware said, there is a difference between Python2 & Python3 that leads to this error:
With Python2, int(str(5/2)) gives you 2.
With Python3, the same gives you: ValueError: invalid literal for int() with base 10: '2.5'
If you need to convert some string that could contain float instead of int, you should always use the following ugly formula:
int(float(myStr))
As float('3.0') and float('3') give you 3.0, but int('3.0') gives you the error.
It might be better to validate a right when it is input.
try:
a = int(input("Enter 'a' "))
except ValueError:
print('PLease input a valid integer')
This either casts a to an int so you can be assured that it is an integer for all later uses or it handles the exception and alerts the user
int() casting can't handle string numbers that have decimal points
- example --> int('13.5') will give you error , but int('13') will convert the
string to integer
Why : This considered as explicit casting required by the user as it prevents you from losing information like 0.5 if you read dataset and don't know it's had floating-point numbers
Work Around >
int(Float("13.5"))
A real-world example I faced: where I wanted the numbers as int while int(I["mpg"]) directly didn't work so I used float() then int()
sum([int(float(i["mpg"])) for i in file])//len(file)

"TypeError: Can't convert 'int' object to str implicitly" Trying to rename files in python3

I wrote the following function to rename a series of JPGs. They come off the scanner as a sequential number, however I need to know what the front and back of each document is (in JPG form). How I chose to do this is make one end in A, and one end in B. For example:
00001.jpg becomes 00001B.jpg
00002.jpg becomes 00001A.jpg
00003.jpg becomes 00002B.jpg
00004.jpg becomes 00002A.jpg
This is because the scanner has a peculiar behavior, where the back is scanned before the front of each document.
I wrote the following function to handle this:
def make_page_denotation(rename_directory, extension, rename_files):
for filename in glob(rename_files):
sequential_number = int(filename[-9:-4])
if sequential_number & 1 == True:
os.rename(filename, filename.replace(str(sequential_number), str(sequential_number) + "B"))
if sequential_number & 1 == False:
os.rename(filename, filename.replace(int(sequential_number), int(sequential_number) - 1))
os.rename(filename, filename.replace(str(sequential_number), str(sequential_number) + "A"))
However, I am getting the following error:
> Traceback (most recent call last):
File "imageqa.py", line 311, in <module>
imageqa_misc.make_page_denotation(rename_directory, extension, rename_files)
File "/home/mylan/workspace/Working/Image-QA/imageqa_misc.py", line 68, in make_page_denotation
os.rename(filename, filename.replace(int(sequential_number), int(sequential_number) - 1))
TypeError: Can't convert 'int' object to str implicitly
I can't figure out, for the life of me why this is happening. I'm still new to Python and programming in general, and am writing a series of scripts to help me out in everyday life to "dive right in" so any help would be greatly appreciated! The odd test (first block) works perfectly on its own, so I'm not sure how I messed up the second block.
0) Don't compare to boolean literals. Just use the condition directly.
1) Don't write "if <something>" and then "if <not something>". That's what else is for.
2) % 2 is a clearer parity test than & 1.
3) Your logic is wrong: you're subtracting 1 from the even numbers and leaving the odd numbers untouched, so you'd get 1, 1, 3, 3, 5, 5... instead of 1, 1, 2, 2, 3, 3 as desired. You need a division by 2 in there. The simple way is to add 1, divide by 2 (in integer arithmetic, this discards the remainder), and then append either an 'A' or a 'B' according to the parity.
4) The error message tells you exactly what's wrong. TypeError: Can't convert 'int' object to str implicitly means exactly what it says. replace replaces some substring in a string with another string. You can't replace an int within the string with another int, because the string doesn't contain ints (it merely contains substrings that can be interpreted as ints), and won't store any ints that you try to put into it.
Even if you could do this, there would be no way for the implicit conversion to know how many leading zeroes to use in the replacement.
The way we deal with this is to convert explicitly, of course. That's what int is doing in your code: explicitly converting the string to an integer. The calls to str in your code are useless, because explicitly converting a string to a string does nothing.
So the logic is:
a) Extract the part we're going to replace, and explicitly convert to int.
b) Do math to get the integer part of the replacement.
c) Use string formatting to create a string that contains (a) a zero-padded string representation of the new integer; (b) either 'A' or 'B' as a suffix.
d) Replace the part we're going to replace with that replacement. Instead of searching for the text again and replacing it, we'll just splice the replacement into where the original was - that's more direct, and avoids mistakes (e.g. if the file name happens to contain another copy of the number earlier on).
def make_page_denotation(rename_directory, extension, rename_files):
for filename in glob(rename_files):
original = int(filename[-9:-4]) # a)
new_value = (original + 1) / 2 # b)
replacement = ('%05d' % new_value) + ('B' if original % 2 else 'A') # c)
new_filename = filename[:-9] + replacement + filename[-4:] # d)
os.rename(filename, replacement)
>>> for fname in "00001.jpg","00002.jpg","00003.jpg","00004.jpg":
... seq=int(fname[-9:-4])
... print "%05d%s.jpg"%((seq+1)/2,"BA"[seq%2])
...
00001A.jpg
00001B.jpg
00002A.jpg
00002B.jpg
In filename.replace(int(sequential_number), int(sequential_number) - 1), the two arguments to str.replace must be strings, you're passing in integers, which the traceback tells you. Try filename.replace(int(sequential_number), str(int(sequential_number) - 1))..
Also, what are you trying to do with sequential_number & 1? Check if the number is even? Why not just sequential_number % 2 == 0? That's clearer to read.
Even if your question is solved now, I have a reommendation:
if sequential_number & 1 == True:
<stuff B>
if sequential_number & 1 == False:
<stuff A>
is quite ugly. Better do this:
if sequential_number & 1:
<stuff B>
else:
<stuff A>

Categories

Resources