为什么在下面的代码中x和y是字符串而不是整数?
(注:在Python 2。X使用raw_input()。在Python 3中。X使用input()。在Python 3.x中raw_input()被重命名为input()
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
多个问题要求在一行上输入多个整数。最好的方法是一行一行地输入整串数字,然后把它们分割成整数。下面是Python 3的版本:
a = []
p = input()
p = p.split()
for i in p:
a.append(int(i))
你也可以使用列表推导式:
p = input().split("whatever the seperator is")
要将所有输入从string转换为int,我们执行以下操作:
x = [int(i) for i in p]
print(x, end=' ')
列表元素应该以直线形式打印。
多个问题要求在一行上输入多个整数。最好的方法是一行一行地输入整串数字,然后把它们分割成整数。下面是Python 3的版本:
a = []
p = input()
p = p.split()
for i in p:
a.append(int(i))
你也可以使用列表推导式:
p = input().split("whatever the seperator is")
要将所有输入从string转换为int,我们执行以下操作:
x = [int(i) for i in p]
print(x, end=' ')
列表元素应该以直线形式打印。