0

How to convert type of variable in Python?

This code did not work:( number=input() print('number to be tested: '+number) int(number) if number>5: result=' is greater than 5' else: result=' is less than 5' str(number) print(number+result)

8th Apr 2018, 12:31 AM
db1070
db1070 - avatar
6 Answers
+ 2
number = int(number) and number = str(number) should do the trick.
8th Apr 2018, 12:41 AM
strawdog
strawdog - avatar
+ 1
Input returns a string turn it into an int: number = int(input())
8th Apr 2018, 12:40 AM
TurtleShell
TurtleShell - avatar
+ 1
which is the best programing language to learn?
8th Apr 2018, 2:27 PM
Sanket Kheni
Sanket Kheni - avatar
0
Also use the format method to print variable values... print("Number to test: {}".format(number)) instead of print("Number to test: " + number) print("{} {}".format(number, result)) instead of print(number + result)
8th Apr 2018, 12:45 AM
TurtleShell
TurtleShell - avatar
0
depends on what you want to do
8th Apr 2018, 5:30 PM
db1070
db1070 - avatar
0
emm, what if the number receives text instead of real numbers?
6th Jun 2024, 6:34 AM
Sergiy Sh