# python 我怎樣才能讀取輸入為整數？

``````x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
``````

python python-2.7 python-3.x int

``````play = True

while play:

x = input("Enter a number: ")
y = input("Enter a number: ")

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)

if input("Play again? ") == "no":
play = False
``````

`input()` （Python 3）和`raw_input()` （Python 2） 總是返回字符串。 用`int()`將結果轉換為整數。

``````x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
``````

``````def dbz():
try:
r = raw_input("Enter number:")
if r.isdigit():
i = int(raw_input("Enter divident:"))
d = int(r)/i
print "O/p is -:",d
else:
print "Not a number"
except Exception ,e:
print "Program halted incorrect data entered",type(e)
dbz()

Or

num = input("Enter Number:")#"input" will accept only numbers
``````

``````my_number = int(input("enter the number"))
``````

``````my_decimalnumber = float(input("enter the number"))
``````

``````num = input()
num1 = 0
num2 = 0

for i in range(len(num)):
if num[i] == ' ':
break

num1 = int(num[:i])
num2 = int(num[i+1:])
``````

``````x = raw_input("Enter a number: ") #String input
x = int(raw_input("Enter a number: ")) #integer input
x = float(raw_input("Enter a float number: ")) #float input
x = eval(raw_input("Enter a float number: ")) #eval input
``````

``````x = input("Enter a number: ") #String input
``````

``````x = int(input("Enter a number: ")) #integer input
``````

``````x = float(input("Enter a float number: ")) #float input
``````

``````x = eval(input("Enter a float number: ")) #eval input
``````

NameError：名稱''未定義

### Tags

python   python-2.7   python-3.x   int