Python maths does not multiply, it uses string instead? [duplicate] - python

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.

Related

Split string into two integers, python [duplicate]

This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want

How to compare user input to a randomly generated number? [duplicate]

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'>

Python: is there a way to 'cleanly' divide two numbers of type float and int? [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4

How can I convert and integer into a string? [duplicate]

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

python 3 How to append number to number [duplicate]

This question already has answers here:
How to concatenate two integers in Python?
(17 answers)
Closed 8 years ago.
How would I append a number to the end of another number?
for example:
a = 1
b = 2
a = a + b
print(a)
Output = 3
I want to get my output as 12 but instead I get 3. I understand that when you do number + number you get the addition of that but I would like to do is append b to a and so I would get 12. I have tried appending a number to a number but I get an error. I think that you can only append a list.
My question is:
How do I append a number to a number?
or is there a better way of doing it?
The reason you get 3 is because a and b contain integers. What you want is string concatenation to get 12. In order to use string concatenation you need strings. You can type cast the integers to string using str() and then use int() to type cast the string to an integer.
a = 1
b = 2
a = str(a) + str(b)
a = int(a)
print(a)
The oneliner solution is already provided in the comments.

Categories

Resources