+ 1

How to get input by the user and use the input on control structure in python

How to use the input value entered by the user on if statements. For example : x = input("enter a num") if x = 1: print("one") else: print("false") the above code is correct or wrong. if wrong tell me how to correct it. sorry for my bad english.

30th May 2018, 2:26 PM
Thameem Ansari
Thameem Ansari - avatar
4 ответов
+ 3
- The indentation is incorrect at line 1 - input() returns a string (str for short) which you can't compare correctly numbers with so turn that into an int however if the user inputs anything other than a number it will throw an error - = operator is used for assigning values/objects use == to compare 2 values e.g. 2 == 3, 6 == 6, 9.8 == 9.9 x = int(input("Enter a number: ")) if x == 1: print("One") else: print("False")
30th May 2018, 2:36 PM
TurtleShell
TurtleShell - avatar
+ 3
x = int(input("enter a num")) if x ==1: print("one") else: print("false")
30th May 2018, 2:38 PM
Markus Kaleton
Markus Kaleton - avatar
0
can you please correct it.
30th May 2018, 2:35 PM
Thameem Ansari
Thameem Ansari - avatar
0
I think because your asigning x as a string, then you comparing it as a int
30th May 2018, 2:36 PM
Henry Arch
Henry Arch - avatar