# python bar大小 - 我怎樣才能讀取輸入為整數？

subplot放大 python座標軸 (13)

`eval(input("Enter a number: "))`

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

``````x=int(input("Enter the number"))
``````

``````arr = map(int, raw_input().split())
``````

``````num1, num2 = map(int, raw_input().split())
``````

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

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

``````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：名稱''未定義

``````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:])
``````

``````input()
``````

``````int(input())
``````

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

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

Python 2.x

``````>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
``````

``````>>> data = eval(raw_input("Enter a number: "))
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
``````

``````>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = raw_input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <type 'str'>)
``````

Python 3.x

Python 3.x的`input`和Python 2.x的`raw_input`相似，並且`raw_input`在Python 3.x中不可用。

``````>>> import sys
>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <class 'str'>)
``````

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

``````>>> data = int(input("Enter a number: "), 8)
Enter a number: 777
>>> data
511
>>> data = int(input("Enter a number: "), 16)
Enter a number: FFFF
>>> data
65535
>>> data = int(input("Enter a number: "), 2)
Enter a number: 10101010101
>>> data
1365
``````

``````>>> data = int(input("Enter a number: "), 2)
Enter a number: 1234
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '1234'
``````

``````while True:
...
...
if input("Play again? ") == "no":
break
``````

PS ：Python不期望`;` 在行尾:)

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

`````` p=raw_input()
p=p.split()
for i in p:
a.append(int(i))
``````

Python 3.x有`input()`函數，它總是返回字符串。所以你必須轉換為`int`

python 3.x

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

python 2.x

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

``````In [1]: from collections import ChainMap
In [2]: from string import ascii_uppercase as up, ascii_lowercase as lo; x = dict(zip(lo, up)); y = dict(zip(up, lo))
In [3]: chainmap_dict = ChainMap(y, x)
In [4]: union_dict = dict(x.items() | y.items())
In [5]: timeit for k in union_dict: union_dict[k]
100000 loops, best of 3: 2.15 µs per loop
In [6]: timeit for k in chainmap_dict: chainmap_dict[k]
10000 loops, best of 3: 27.1 µs per loop
``````