This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
So I made a "Guess the Number" program with Python and tested it. Apparently, choosing the correct number still comes up as the incorrect else statement. How can I fix this?
As you can see the 3 I entered apparently isn't the same as the 3 my program came up:
You're comparing the return value of your call to input (the string in your variable usernum) with the return value of random.randint which is an integer in your variable EasyRN.
You'll need to convert either the integer into an string:
usernum = int(usernum)
Or the string into an integer:
EasyRN = str(EasyRN)
Afterwards, you can use == to compare them.
The result of input() is text (in Python, a str, short for the word "string" which is used in programming), while the output of random.randint() is a number (an int, short for "integer").
>>> type("3")
<class 'str'>
>>> type(3)
<class 'int'>
If you compare a str and an int they will never be equivalent, as it's an apples-to-oranges comparison.
Look at the int() function which converts a string to an integer.
>>> int("3")
3
>>> type(int("3"))
<class 'int'>
Related
This question already has answers here:
How are strings compared?
(7 answers)
Closed 5 months ago.
I came up with this comparison issue in python in binary values which is mentioned here in the image.
What are the possible reasons for returning "True" to the comparison between bin(1538)<bin(5138) since numerical value and binary digit length of the two binary values are contradictory to the output in python? Are there any specific comparison rules in python when it comes to the comparisons between binary values generated by bin() function (python default function) or are they similar as normal number comparisons?
because you are comparing two strings and the first string is "larger" than the second one...
try this as an example:
x = bin(1538)
print(x)
print(type(x))
y = bin(5138)
print(y)
print(type(y))
returns this:
0b11000000010
<class 'str'>
0b1010000010010
<class 'str'>
This question already has answers here:
Python: Strip off string quotes from binary number
(5 answers)
Closed 1 year ago.
b = 15
a = bin(b) # I want return as an integer not string
print(a, type(a)) # output is string, actually I want is integer
# output - 0b1111 <class 'str'>
So, I want to get bin() function return as an integer
The int function is used to convert to the integer. We need to pass the number and its base to convert it into an integer (since, the base for binary values is 2).
a = int('101',2)
print(a)
If you question is about converting for example 5 into bin in python, the bin function actually gives 0b101 as the result. So the simple trick to get 101 as an int is 👇
intnum=int(bin(number)[2:])
This question already has answers here:
How to convert hex string to hex number?
(8 answers)
Closed 7 years ago.
I'm having problems converting a hex string like this: 0x187c to a hex integer without loosing zeros, and converting to integer.
I need the exact hex integer to find USB devices. The hex integers correspond to a vendor and product id.
I am using the following code to find devices:
dev = usb.core.find(idVendor=0x187c, idProduct=0x0521)
The problem is that I need to enter those numbers from a graphical interface. However, once the user types them I get them back as a string, and the usb.core module doesn't find the device.
>>> type(0x187c)
<type 'int'>
>>> type("0x187c")
<type 'str'>
>>> type(hex(int("0x187c",16)))
<type 'str'>
Is there any way to retrieve this value that will work with usb.core?
There are no "hex integers". An integer is an integer. It isn't important for your computer if it was originally written in hex, octal, binary or decimal.
dev = usb.core.find(idVendor=int(vendor, 16), idProduct=int(product, 16))
will simply work.
Take off the call to hex..
>>> type(int('0x187c', 16))
<type 'int'>
>>> print 0x187c
6268
>>> print int('0x187c', 16)
6268
>>> print int('0x187c', 16) == 0x187c
True
If int('0x187c', 16) isn't working then the problem is elsewhere..
This question already has answers here:
How to print list item + integer/string using logging in Python
(2 answers)
Closed 7 years ago.
I am coding in Python, and have reached an error that I cannot seem to solve. Here's the part of the code that it affects.
import random
a = raw_input("Enter text")
b = random.randrange(1,101)
print (a+b)
When I try to run the code, I get the error "TypeError: cannot concatenate 'str' and 'int' objects"
I want to know how to print the result of a+b.
To answer to the question in the title, you can convert an integer into a string with str. But the print function already applies str to its argument, in order to be able to print it.
Here, your problem comes from the fact that a is a string while b is an integer. The + operator works on two strings, or two ints, but not a combination of this two types. If you have two strings, the + will mean concatenate. If you have two ints, the + will mean add. It then depends on the result you want to get.
You can convert a string to an integer by using int.
Try this code:
import random
a = int (raw_input ("Enter int "))
b = random.randrange (1, 101)
print a + b
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 8 years ago.
I was just testing a little python maths and I could not multiply numbers! I am really confused because I thought this simple code would work:
test = raw_input("answer")
new = test * 5
print new
Instead, it just gave whatever I wrote five times next to each other. E.g I write 8 and it prints 88888! Can somebody explain this?
You need to cast to int, raw_input returns a string:
test = int(raw_input("answer"))
You can see the type is str without casting:
In [5]: test = raw_input("answer ")
answer 8
In [6]: type(test)
Out[6]: str
In [7]: test = int(raw_input("answer "))
answer 8
In [8]: type(test)
Out[8]: int
When you multiply the string python will return the string repeated test times.